nghuuphuoc 12 years ago
parent
commit
600d139dda

+ 1 - 0
CHANGELOG.md

@@ -7,6 +7,7 @@
 * [#34: Avoid from calling form submit recursively](https://github.com/nghuuphuoc/bootstrapvalidator/issues/34)
 * [#39: Validate existing fields only](https://github.com/nghuuphuoc/bootstrapvalidator/issues/39)
 * [#40: Fix the issue when the form label doesn't have class](https://github.com/nghuuphuoc/bootstrapvalidator/issues/40)
+* [#43: Only validate not empty field](https://github.com/nghuuphuoc/bootstrapvalidator/issues/43)
 
 ##v0.2.1 (2013-11-08)
 

+ 0 - 3
demo/index.html

@@ -102,9 +102,6 @@ $(document).ready(function() {
             },
             email: {
                 validators: {
-                    notEmpty: {
-                        message: 'The email address is required and can\'t be empty'
-                    },
                     emailAddress: {
                         message: 'The input is not a valid email address'
                     }

+ 78 - 16
dist/js/bootstrapValidator.js

@@ -383,7 +383,12 @@
          * @returns {Boolean}
          */
         validate: function(validator, $field, options) {
-            var value = parseFloat($field.val());
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
+            value = parseFloat(value);
             return (options.inclusive === true)
                         ? (value > options.min && value < options.max)
                         : (value >= options.min && value <= options.max);
@@ -408,6 +413,10 @@
          */
         validate: function(validator, $field, options) {
             var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
             if (options.callback && 'function' == typeof options.callback) {
                 return options.callback.call(this, value, this);
             }
@@ -429,6 +438,9 @@
          */
         validate: function(validator, $field, options) {
             var value = $field.val();
+            if (value == '') {
+                return true;
+            }
 
             // Accept only digits, dashes or spaces
             if (/[^0-9-\s]+/.test(value)) {
@@ -469,8 +481,12 @@
          * @returns {Boolean}
          */
         validate: function(validator, $field, options) {
-            var value        = $field.val(),
-                $compareWith = validator.getFieldElement(options.field);
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
+            var $compareWith = validator.getFieldElement(options.field);
             if ($compareWith && value != $compareWith.val()) {
                 validator.removeError($compareWith);
                 return true;
@@ -491,7 +507,12 @@
          * @returns {Boolean}
          */
         validate: function(validator, $field, options) {
-            return /^\d+$/.test($field.val());
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
+            return /^\d+$/.test(value);
         }
     }
 }(window.jQuery));
@@ -506,10 +527,14 @@
          * @returns {Boolean}
          */
         validate: function(validator, $field, options) {
-            var value = $field.val(),
-                // Email address regular expression
-                // http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
-                emailRegExp = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
+            // Email address regular expression
+            // http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
+            var emailRegExp = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
             return emailRegExp.test(value);
         }
     }
@@ -528,7 +553,11 @@
          * @returns {Boolean}
          */
         validate: function(validator, $field, options) {
-            var value = parseFloat($field.val());
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+            value = parseFloat(value);
             return (options.inclusive === true) ? (value > options.value) : (value >= options.value);
         }
     }
@@ -546,6 +575,9 @@
          */
         validate: function(validator, $field, options) {
             var value = $field.val();
+            if (value == '') {
+                return true;
+            }
             return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(value);
         }
     };
@@ -562,8 +594,12 @@
          * @returns {Boolean}
          */
         validate: function(validator, $field, options) {
-            var value        = $field.val(),
-                $compareWith = validator.getFieldElement(options.field);
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
+            var $compareWith = validator.getFieldElement(options.field);
             if ($compareWith && value == $compareWith.val()) {
                 validator.removeError($compareWith);
                 return true;
@@ -587,7 +623,11 @@
          * @returns {Boolean}
          */
         validate: function(validator, $field, options) {
-            var value = parseFloat($field.val());
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+            value = parseFloat(value);
             return (options.inclusive === true) ? (value < options.value) : (value <= options.value);
         }
     };
@@ -621,6 +661,10 @@
          */
         validate: function(validator, $field, options) {
             var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
             return options.regexp.test(value);
         }
     };
@@ -639,10 +683,15 @@
          *      <fieldName>: <fieldValue>
          *  }
          * - message: The invalid message
-         * @returns {String}
+         * @returns {Boolean|String}
          */
         validate: function(validator, $field, options) {
-            var value = $field.val(), name = $field.attr('name'), data = options.data;
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
+            var name = $field.attr('name'), data = options.data;
             if (data == null) {
                 data = {};
             }
@@ -676,7 +725,12 @@
          * @returns {Boolean}
          */
         validate: function(validator, $field, options) {
-            var value = $.trim($field.val()), length = value.length;
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
+            var length = $.trim(value).length;
             if ((options.min && length < options.min) || (options.max && length > options.max)) {
                 return false;
             }
@@ -696,6 +750,11 @@
          * @returns {Boolean}
          */
         validate: function(validator, $field, options) {
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
             // Credit to https://gist.github.com/dperini/729294
             //
             // Regular Expression for URL validation
@@ -769,7 +828,7 @@
                 "(?:/[^\\s]*)?" +
                 "$", "i"
             );
-            return urlExp.test($field.val());
+            return urlExp.test(value);
         }
     };
 }(window.jQuery));
@@ -785,6 +844,9 @@
          */
         validate: function(validateInstance, $field, options) {
             var value = $field.val();
+            if (value == '') {
+                return true;
+            }
             return /^\d{5}([\-]\d{4})?$/.test(value);
         }
     };

File diff suppressed because it is too large
+ 1 - 1
dist/js/bootstrapValidator.min.js


+ 6 - 1
src/js/validator/between.js

@@ -13,7 +13,12 @@
          * @returns {Boolean}
          */
         validate: function(validator, $field, options) {
-            var value = parseFloat($field.val());
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
+            value = parseFloat(value);
             return (options.inclusive === true)
                         ? (value > options.min && value < options.max)
                         : (value >= options.min && value <= options.max);

+ 4 - 0
src/js/validator/callback.js

@@ -16,6 +16,10 @@
          */
         validate: function(validator, $field, options) {
             var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
             if (options.callback && 'function' == typeof options.callback) {
                 return options.callback.call(this, value, this);
             }

+ 3 - 0
src/js/validator/creditCard.js

@@ -12,6 +12,9 @@
          */
         validate: function(validator, $field, options) {
             var value = $field.val();
+            if (value == '') {
+                return true;
+            }
 
             // Accept only digits, dashes or spaces
             if (/[^0-9-\s]+/.test(value)) {

+ 6 - 2
src/js/validator/different.js

@@ -10,8 +10,12 @@
          * @returns {Boolean}
          */
         validate: function(validator, $field, options) {
-            var value        = $field.val(),
-                $compareWith = validator.getFieldElement(options.field);
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
+            var $compareWith = validator.getFieldElement(options.field);
             if ($compareWith && value != $compareWith.val()) {
                 validator.removeError($compareWith);
                 return true;

+ 6 - 1
src/js/validator/digits.js

@@ -9,7 +9,12 @@
          * @returns {Boolean}
          */
         validate: function(validator, $field, options) {
-            return /^\d+$/.test($field.val());
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
+            return /^\d+$/.test(value);
         }
     }
 }(window.jQuery));

+ 8 - 4
src/js/validator/emailAddress.js

@@ -9,10 +9,14 @@
          * @returns {Boolean}
          */
         validate: function(validator, $field, options) {
-            var value = $field.val(),
-                // Email address regular expression
-                // http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
-                emailRegExp = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
+            // Email address regular expression
+            // http://stackoverflow.com/questions/46155/validate-email-address-in-javascript
+            var emailRegExp = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
             return emailRegExp.test(value);
         }
     }

+ 5 - 1
src/js/validator/greaterThan.js

@@ -12,7 +12,11 @@
          * @returns {Boolean}
          */
         validate: function(validator, $field, options) {
-            var value = parseFloat($field.val());
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+            value = parseFloat(value);
             return (options.inclusive === true) ? (value > options.value) : (value >= options.value);
         }
     }

+ 3 - 0
src/js/validator/hexColor.js

@@ -11,6 +11,9 @@
          */
         validate: function(validator, $field, options) {
             var value = $field.val();
+            if (value == '') {
+                return true;
+            }
             return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(value);
         }
     };

+ 6 - 2
src/js/validator/identical.js

@@ -10,8 +10,12 @@
          * @returns {Boolean}
          */
         validate: function(validator, $field, options) {
-            var value        = $field.val(),
-                $compareWith = validator.getFieldElement(options.field);
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
+            var $compareWith = validator.getFieldElement(options.field);
             if ($compareWith && value == $compareWith.val()) {
                 validator.removeError($compareWith);
                 return true;

+ 5 - 1
src/js/validator/lessThan.js

@@ -12,7 +12,11 @@
          * @returns {Boolean}
          */
         validate: function(validator, $field, options) {
-            var value = parseFloat($field.val());
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+            value = parseFloat(value);
             return (options.inclusive === true) ? (value < options.value) : (value <= options.value);
         }
     };

+ 4 - 0
src/js/validator/regexp.js

@@ -11,6 +11,10 @@
          */
         validate: function(validator, $field, options) {
             var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
             return options.regexp.test(value);
         }
     };

+ 7 - 2
src/js/validator/remote.js

@@ -12,10 +12,15 @@
          *      <fieldName>: <fieldValue>
          *  }
          * - message: The invalid message
-         * @returns {String}
+         * @returns {Boolean|String}
          */
         validate: function(validator, $field, options) {
-            var value = $field.val(), name = $field.attr('name'), data = options.data;
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
+            var name = $field.attr('name'), data = options.data;
             if (data == null) {
                 data = {};
             }

+ 6 - 1
src/js/validator/stringLength.js

@@ -12,7 +12,12 @@
          * @returns {Boolean}
          */
         validate: function(validator, $field, options) {
-            var value = $.trim($field.val()), length = value.length;
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
+            var length = $.trim(value).length;
             if ((options.min && length < options.min) || (options.max && length > options.max)) {
                 return false;
             }

+ 6 - 1
src/js/validator/uri.js

@@ -9,6 +9,11 @@
          * @returns {Boolean}
          */
         validate: function(validator, $field, options) {
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
             // Credit to https://gist.github.com/dperini/729294
             //
             // Regular Expression for URL validation
@@ -82,7 +87,7 @@
                 "(?:/[^\\s]*)?" +
                 "$", "i"
             );
-            return urlExp.test($field.val());
+            return urlExp.test(value);
         }
     };
 }(window.jQuery));

+ 3 - 0
src/js/validator/usZipCode.js

@@ -10,6 +10,9 @@
          */
         validate: function(validateInstance, $field, options) {
             var value = $field.val();
+            if (value == '') {
+                return true;
+            }
             return /^\d{5}([\-]\d{4})?$/.test(value);
         }
     };