Browse Source

#233: Fix the issue that threshold option doesn't have effect on fields

phuoc 11 years ago
parent
commit
674e871570
3 changed files with 48 additions and 6 deletions
  1. 23 2
      dist/js/bootstrapValidator.js
  2. 2 2
      dist/js/bootstrapValidator.min.js
  3. 23 2
      src/js/bootstrapValidator.js

+ 23 - 2
dist/js/bootstrapValidator.js

@@ -387,7 +387,9 @@
                 case 'enabled':
                 default:
                     $field.off(events).on(events, function() {
-                        that.validateFieldElement($(this));
+                        if (that._exceedThreshold($(this))) {
+                            that.validateFieldElement($(this));
+                        }
                     });
                     break;
             }
@@ -467,6 +469,23 @@
 
             return false;
         },
+
+        /**
+         * Check if the number of characters of field value exceed the threshold or not
+         *
+         * @param {jQuery} $field The field element
+         * @returns {Boolean}
+         */
+        _exceedThreshold: function($field) {
+            var field     = $field.attr('data-bv-field'),
+                threshold = this.options.fields[field].threshold || this.options.threshold;
+            if (!threshold) {
+                return true;
+            }
+            var type       = $field.attr('type'),
+                cannotType = ['button', 'checkbox', 'file', 'hidden', 'image', 'radio', 'reset', 'submit'].indexOf(type) != -1;
+            return (cannotType || $field.val().length >= threshold);
+        },
         
         // --- Events ---
 
@@ -498,7 +517,9 @@
 
                             for (var i = 0; i < fields.length; i++) {
                                 $(fields[i]).off(events).on(events, function() {
-                                    that.validateFieldElement($(this));
+                                    if (that._exceedThreshold($(this))) {
+                                        that.validateFieldElement($(this));
+                                    }
                                 });
                             }
                         }

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


+ 23 - 2
src/js/bootstrapValidator.js

@@ -386,7 +386,9 @@
                 case 'enabled':
                 default:
                     $field.off(events).on(events, function() {
-                        that.validateFieldElement($(this));
+                        if (that._exceedThreshold($(this))) {
+                            that.validateFieldElement($(this));
+                        }
                     });
                     break;
             }
@@ -466,6 +468,23 @@
 
             return false;
         },
+
+        /**
+         * Check if the number of characters of field value exceed the threshold or not
+         *
+         * @param {jQuery} $field The field element
+         * @returns {Boolean}
+         */
+        _exceedThreshold: function($field) {
+            var field     = $field.attr('data-bv-field'),
+                threshold = this.options.fields[field].threshold || this.options.threshold;
+            if (!threshold) {
+                return true;
+            }
+            var type       = $field.attr('type'),
+                cannotType = ['button', 'checkbox', 'file', 'hidden', 'image', 'radio', 'reset', 'submit'].indexOf(type) != -1;
+            return (cannotType || $field.val().length >= threshold);
+        },
         
         // --- Events ---
 
@@ -497,7 +516,9 @@
 
                             for (var i = 0; i < fields.length; i++) {
                                 $(fields[i]).off(events).on(events, function() {
-                                    that.validateFieldElement($(this));
+                                    if (that._exceedThreshold($(this))) {
+                                        that.validateFieldElement($(this));
+                                    }
                                 });
                             }
                         }