Browse Source

#18: Add Different validator

nghuuphuoc 12 years ago
parent
commit
7532463b64
6 changed files with 70 additions and 1 deletions
  1. 1 0
      CHANGELOG.md
  2. 10 0
      README.md
  3. 12 0
      demo/index.html
  4. 23 0
      dist/js/bootstrapValidator.js
  5. 1 1
      dist/js/bootstrapValidator.min.js
  6. 23 0
      src/js/validator/different.js

+ 1 - 0
CHANGELOG.md

@@ -2,6 +2,7 @@
 
 __v0.2.0__
 * Add custom submit handler (using ```submitHandler``` option)
+* Add ```different``` validator
 
 __v0.1.1 (2013-10-17)__
 * Add ```submitButtons``` option

+ 10 - 0
README.md

@@ -76,6 +76,7 @@ Below is the list of built-in validators sorted in alphabetical order:
 Validator name                          | Description
 ----------------------------------------|------------
 [between](#between-validator)           | Check if the input value is between (strictly or not) two given numbers
+[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
 emailAddress                            | Validate an email address
 [greaterThan](#greaterthan-validator)   | Return true if the value is greater than or equals to given number
@@ -91,6 +92,8 @@ usZipCode                               | Validate a US zip code
 
 The validator options are described in the following section:
 
+(*the option masked with (*) is required*)
+
 ### Between Validator
 
 Option name | Default | Description
@@ -100,6 +103,13 @@ min         | n/a     | The lower value in the range. This option is required
 max         | n/a     | The upper value in the range. This option is required
 inclusive   | true    | Can be true or false. If true, the input value must be in the range strictly
 
+### Different Validator
+
+Option name | Default | Description
+------------|---------|------------
+message     | n/a     | The error message
+field (*)   | n/a     | The name of field that will be used to compare with current one
+
 ### GreaterThan Validator
 
 | Option name | Default | Description

+ 12 - 0
demo/index.html

@@ -80,6 +80,10 @@ $(document).ready(function() {
                     regexp: {
                         regexp: /^[a-zA-Z0-9_\.]+$/,
                         message: 'The username can only consist of alphabetical, number, dot and underscore'
+                    },
+                    different: {
+                        field: 'password',
+                        message: 'The username and password can\'t be the same as each other'
                     }
                 }
             },
@@ -101,6 +105,10 @@ $(document).ready(function() {
                     identical: {
                         field: 'confirmPassword',
                         message: 'The password and its confirm are not the same'
+                    },
+                    different: {
+                        field: 'username',
+                        message: 'The password can\'t be the same as username'
                     }
                 }
             },
@@ -112,6 +120,10 @@ $(document).ready(function() {
                     identical: {
                         field: 'password',
                         message: 'The password and its confirm are not the same'
+                    },
+                    different: {
+                        field: 'username',
+                        message: 'The password can\'t be the same as username'
                     }
                 }
             }

+ 23 - 0
dist/js/bootstrapValidator.js

@@ -339,6 +339,29 @@
     };
 }(window.jQuery));
 ;(function($) {
+    $.fn.bootstrapValidator.validators.different = {
+        /**
+         * Return true if the input value is different with given field's value
+         *
+         * @param {BootstrapValidator} validator The validator plugin instance
+         * @param {jQuery} $field Field element
+         * @param {Object} options Consists of the following key:
+         * - field: The name of field that will be used to compare with current one
+         * @returns {boolean}
+         */
+        validate: function(validator, $field, options) {
+            var value        = $field.val(),
+                $compareWith = validator.getFieldElement(options.field);
+            if ($compareWith && value != $compareWith.val()) {
+                validator.removeError($compareWith);
+                return true;
+            } else {
+                return false;
+            }
+        }
+    };
+}(window.jQuery));
+;(function($) {
     $.fn.bootstrapValidator.validators.digits = {
         /**
          * Return true if the input value contains digits only

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


+ 23 - 0
src/js/validator/different.js

@@ -0,0 +1,23 @@
+(function($) {
+    $.fn.bootstrapValidator.validators.different = {
+        /**
+         * Return true if the input value is different with given field's value
+         *
+         * @param {BootstrapValidator} validator The validator plugin instance
+         * @param {jQuery} $field Field element
+         * @param {Object} options Consists of the following key:
+         * - field: The name of field that will be used to compare with current one
+         * @returns {boolean}
+         */
+        validate: function(validator, $field, options) {
+            var value        = $field.val(),
+                $compareWith = validator.getFieldElement(options.field);
+            if ($compareWith && value != $compareWith.val()) {
+                validator.removeError($compareWith);
+                return true;
+            } else {
+                return false;
+            }
+        }
+    };
+}(window.jQuery));