Browse Source

Add between validator including document

phuoc 12 years ago
parent
commit
5ce8a5844f
5 changed files with 108 additions and 18 deletions
  1. 20 13
      README.md
  2. 28 4
      dist/js/bootstrapvalidate.js
  3. 1 1
      dist/js/bootstrapvalidate.min.js
  4. 35 0
      docs/between/README.md
  5. 24 0
      src/js/validator/between.js

+ 20 - 13
README.md

@@ -8,19 +8,7 @@ The plugin uses [Bootstrap Tooltip](http://getbootstrap.com/javascript/#tooltips
 
 
 ## Features
 ## Features
 
 
-The plugin provides built-in validators which are listed below in alphabetical order:
-
-Validator name | Description
----------------|------------
-digits         | Return true if the value contains only digits
-emailAddress   | Validate an email address
-greaterThan    | Return true if the value is greater than or equals to given number
-identical      | Check if the value is the same as one of particular field
-lessThan       | Return true if the value is less than or equals to given number
-notEmpty       | Check if the value is empty
-regexp         | Check if the value matches given Javascript regular expression
-stringLength   | Validate the length of a string
-uri            | Validate an URL address
+* Many built-in [validators](#validators)
 
 
 ## Required
 ## Required
 
 
@@ -71,6 +59,25 @@ $(document).ready(function() {
 });
 });
 ```
 ```
 
 
+The ```<validatorName>``` can be the name of the built-in validator which are described in the [Validators](#validators) section
+
+## Validators
+
+Below is the list of built-in validators sorted in alphabetical order:
+
+Validator name                            | Description
+------------------------------------------|------------
+[between](docs/between/README.md)         | Checks if the input value is between (strictly or not) two given numbers
+digits                                    | Return true if the value contains only digits
+emailAddress                              | Validate an email address
+greaterThan                               | Return true if the value is greater than or equals to given number
+identical                                 | Check if the value is the same as one of particular field
+lessThan                                  | Return true if the value is less than or equals to given number
+notEmpty                                  | Check if the value is empty
+regexp                                    | Check if the value matches given Javascript regular expression
+stringLength                              | Validate the length of a string
+uri                                       | Validate an URL address
+
 ## Build
 ## Build
 
 
 BootstrapValidate uses [grunt](http://gruntjs.com) to simplify building process.
 BootstrapValidate uses [grunt](http://gruntjs.com) to simplify building process.

+ 28 - 4
dist/js/bootstrapvalidate.js

@@ -173,6 +173,30 @@
 }(window.jQuery));
 }(window.jQuery));
 ;(function($) {
 ;(function($) {
     $.extend($.bootstrapValidator.validator, {
     $.extend($.bootstrapValidator.validator, {
+        between: {
+            /**
+             * Return true if the input value is between (strictly or not) two given numbers
+             *
+             * @param {bootstrapValidator} validateInstance Validate plugin instance
+             * @param {HTMLElement} element
+             * @param {Object} options Can consist of the following keys:
+             * - min
+             * - max
+             * - inclusive [optional]: Can be true or false. Default is true
+             * - message: The invalid message
+             * @returns {boolean}
+             */
+            validate: function(validateInstance, element, options) {
+                var value = parseFloat($(element).val());
+                return (options.inclusive === true)
+                            ? (value > options.min && value < options.max)
+                            : (value >= options.min && value <= options.max);
+            }
+        }
+    });
+}(window.jQuery));
+;(function($) {
+    $.extend($.bootstrapValidator.validator, {
         digits: {
         digits: {
             /**
             /**
              * Return true if the input value contains digits only
              * Return true if the input value contains digits only
@@ -219,13 +243,13 @@
              * @param {HTMLElement} element
              * @param {HTMLElement} element
              * @param {Object} options Can consist of the following keys:
              * @param {Object} options Can consist of the following keys:
              * - value: The number used to compare to
              * - value: The number used to compare to
-             * - strict [optional]: Can be true or false. Default is true
+             * - inclusive [optional]: Can be true or false. Default is true
              * - message: The invalid message
              * - message: The invalid message
              * @returns {boolean}
              * @returns {boolean}
              */
              */
             validate: function(validateInstance, element, options) {
             validate: function(validateInstance, element, options) {
                 var value = parseFloat($(element).val());
                 var value = parseFloat($(element).val());
-                return (options.strict === true) ? (value > options.value) : (value >= options.value);
+                return (options.inclusive === true) ? (value > options.value) : (value >= options.value);
             }
             }
         }
         }
     });
     });
@@ -265,13 +289,13 @@
              * @param {HTMLElement} element
              * @param {HTMLElement} element
              * @param {Object} options Can consist of the following keys:
              * @param {Object} options Can consist of the following keys:
              * - value: The number used to compare to
              * - value: The number used to compare to
-             * - strict [optional]: Can be true or false. Default is true
+             * - inclusive [optional]: Can be true or false. Default is true
              * - message: The invalid message
              * - message: The invalid message
              * @returns {boolean}
              * @returns {boolean}
              */
              */
             validate: function(validateInstance, element, options) {
             validate: function(validateInstance, element, options) {
                 var value = parseFloat($(element).val());
                 var value = parseFloat($(element).val());
-                return (options.strict === true) ? (value < options.value) : (value <= options.value);
+                return (options.inclusive === true) ? (value < options.value) : (value <= options.value);
             }
             }
         }
         }
     });
     });

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


+ 35 - 0
docs/between/README.md

@@ -0,0 +1,35 @@
+# between Validator
+
+This validator checks if the input value is between (strictly or not) two given numbers
+
+## Usage
+
+```javascript
+$(document).ready(function() {
+    $(<form Selector>).bootstrapValidate({
+        fields: {
+            <fieldName>: {
+                validator: {
+                    between: {
+                        message: 'The input value is not between <min> and <max>',
+                        min: <min>,
+                        max: <max>,
+                        inclusive: true
+                    }
+                }
+            }
+        }
+    }
+});
+```
+
+## Options
+
+The ```between``` validator has the following options:
+
+Option name | Type    | Required | Default | Description
+------------|---------|----------|---------|------------
+message     | string  | Yes      | n/a     | The error message
+min         | int     | Yes      | n/a     | The lower value in the range
+max         | int     | Yes      | n/a     | The upper value in the range
+inclusive   | boolean | No       | false   | If true, the input value must be in the range strictly

+ 24 - 0
src/js/validator/between.js

@@ -0,0 +1,24 @@
+(function($) {
+    $.extend($.bootstrapValidator.validator, {
+        between: {
+            /**
+             * Return true if the input value is between (strictly or not) two given numbers
+             *
+             * @param {bootstrapValidator} validateInstance Validate plugin instance
+             * @param {HTMLElement} element
+             * @param {Object} options Can consist of the following keys:
+             * - min
+             * - max
+             * - inclusive [optional]: Can be true or false. Default is true
+             * - message: The invalid message
+             * @returns {boolean}
+             */
+            validate: function(validateInstance, element, options) {
+                var value = parseFloat($(element).val());
+                return (options.inclusive === true)
+                            ? (value > options.min && value < options.max)
+                            : (value >= options.min && value <= options.max);
+            }
+        }
+    });
+}(window.jQuery));