ソースを参照

#13: Add US Zip Code validator

phuoc 12 年 前
コミット
5bc5d45385

+ 14 - 12
README.md

@@ -65,18 +65,20 @@ The ```<validatorName>``` can be the name of the built-in validator which are de
 
 Below is the list of built-in validators sorted in alphabetical order:
 
-Validator name                            | Description
-------------------------------------------|------------
-[between](docs/between)                   | 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
+Validator name          | Description
+------------------------|------------
+[between](docs/between) | 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
+hexColor                | Validate a hex color
+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
+usZipCode               | Validate a US zip code
 
 ## Build
 

+ 15 - 1
demo/validators.html

@@ -73,6 +73,13 @@
                                     <input type="text" class="form-control" name="color" />
                                 </div>
                             </div>
+
+                            <div class="form-group">
+                                <label class="col-lg-3 control-label">US zip code</label>
+                                <div class="col-lg-3">
+                                    <input type="text" class="form-control" name="zipCode" />
+                                </div>
+                            </div>
                         </fieldset>
 
                         <fieldset>
@@ -106,7 +113,7 @@
 
                         <div class="form-group">
                             <div class="col-lg-9 col-lg-offset-3">
-                                <button type="submit" class="btn btn-primary">Sign up</button>
+                                <button type="submit" class="btn btn-primary">Submit</button>
                             </div>
                         </div>
                     </form>
@@ -175,6 +182,13 @@ $(document).ready(function() {
                     }
                 }
             },
+            zipCode: {
+                validator: {
+                    usZipCode: {
+                        message: 'The input is not a valid US zip code'
+                    }
+                }
+            },
             password: {
                 validator: {
                     notEmpty: {

+ 18 - 0
dist/js/bootstrapvalidate.js

@@ -474,3 +474,21 @@
         }
     });
 }(window.jQuery));
+;(function($) {
+    $.extend($.bootstrapValidator.validator, {
+        usZipCode: {
+            /**
+             * Return true if and only if the input value is a valid US zip code
+             *
+             * @param {bootstrapValidator} validateInstance Validate plugin instance
+             * @param {HTMLElement} element
+             * @param {Object} options
+             * @returns {boolean}
+             */
+            validate: function(validateInstance, element, options) {
+                var value = $(element).val();
+                return /^\d{5}([\-]\d{4})?$/.test(value);
+            }
+        }
+    });
+}(window.jQuery));

ファイルの差分が大きいため隠しています
+ 1 - 1
dist/js/bootstrapvalidate.min.js


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

@@ -0,0 +1,18 @@
+(function($) {
+    $.extend($.bootstrapValidator.validator, {
+        usZipCode: {
+            /**
+             * Return true if and only if the input value is a valid US zip code
+             *
+             * @param {bootstrapValidator} validateInstance Validate plugin instance
+             * @param {HTMLElement} element
+             * @param {Object} options
+             * @returns {boolean}
+             */
+            validate: function(validateInstance, element, options) {
+                var value = $(element).val();
+                return /^\d{5}([\-]\d{4})?$/.test(value);
+            }
+        }
+    });
+}(window.jQuery));