Browse Source

#26, #27, #67: Add choice validator

nghuuphuoc 12 years ago
parent
commit
c5f00c956b
5 changed files with 63 additions and 1 deletions
  1. 1 0
      CHANGELOG.md
  2. 9 0
      README.md
  3. 26 0
      dist/js/bootstrapValidator.js
  4. 1 1
      dist/js/bootstrapValidator.min.js
  5. 26 0
      src/js/validator/choice.js

+ 1 - 0
CHANGELOG.md

@@ -5,6 +5,7 @@
 __New features__:
 
 * [#44: Rewrite entirely using Deferred](https://github.com/nghuuphuoc/bootstrapvalidator/issues/44)
+* #26, #27, #67: Add choice validator
 * [#64: Support Danish zip code](https://github.com/nghuuphuoc/bootstrapvalidator/issues/64)
 * [#65: Support Sweden zip code](https://github.com/nghuuphuoc/bootstrapvalidator/issues/64)
 * [#71: Show all errors](https://github.com/nghuuphuoc/bootstrapvalidator/issues/71)

+ 9 - 0
README.md

@@ -118,6 +118,7 @@ Validator name                          | Description
 ----------------------------------------|------------
 [between](#between-validator)           | Check if the input value is between (strictly or not) two given numbers
 [callback](#callback-validator)         | Return the validity from a callback method
+[choice](#choice-validator)             | Check if the number of checked boxes are less or more than a given number
 creditCard                              | Validate a credit card number
 [different](#different-validator)       | Return true if the input value is different with given field's value
 digits                                  | Return true if the value contains only digits
@@ -165,6 +166,14 @@ function(fieldValue, validator) {
 }
 ```
 
+### Choice Validator
+
+Option name | Default | Description
+------------|---------|------------
+message     | n/a     | The error message
+min         | n/a     | The minimum number of check boxes required to be checked
+max         | n/a     | The maximum number of check boxes required to be checked. At least one of ```min``` and ```max``` is required
+
 ### Different Validator
 
 Option name | Default | Description

+ 26 - 0
dist/js/bootstrapValidator.js

@@ -398,6 +398,32 @@
     };
 }(window.jQuery));
 ;(function($) {
+    $.fn.bootstrapValidator.validators.choice = {
+        /**
+         * Check if the number of checked boxes are less or more than a given number
+         *
+         * @param {BootstrapValidator} validator The validator plugin instance
+         * @param {jQuery} $field Field element
+         * @param {Object} options Consists of following keys:
+         * - min
+         * - max
+         * At least one of two keys is required
+         * @returns {Boolean}
+         */
+        validate: function(validator, $field, options) {
+            var numChoices = validator
+                                    .getFieldElements($field.attr('name'))
+                                    .filter(':checked')
+                                    .length;
+            if ((options.min && numChoices < options.min) || (options.max && numChoices > options.max)) {
+                return false;
+            }
+
+            return true;
+        }
+    };
+}(window.jQuery));
+;(function($) {
     $.fn.bootstrapValidator.validators.creditCard = {
         /**
          * Return true if the input value is valid credit card number

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


+ 26 - 0
src/js/validator/choice.js

@@ -0,0 +1,26 @@
+(function($) {
+    $.fn.bootstrapValidator.validators.choice = {
+        /**
+         * Check if the number of checked boxes are less or more than a given number
+         *
+         * @param {BootstrapValidator} validator The validator plugin instance
+         * @param {jQuery} $field Field element
+         * @param {Object} options Consists of following keys:
+         * - min
+         * - max
+         * At least one of two keys is required
+         * @returns {Boolean}
+         */
+        validate: function(validator, $field, options) {
+            var numChoices = validator
+                                    .getFieldElements($field.attr('name'))
+                                    .filter(':checked')
+                                    .length;
+            if ((options.min && numChoices < options.min) || (options.max && numChoices > options.max)) {
+                return false;
+            }
+
+            return true;
+        }
+    };
+}(window.jQuery));