Browse Source

#64: Support Danish zip code

nghuuphuoc 11 years ago
parent
commit
1f66ea75f5

+ 8 - 1
CHANGELOG.md

@@ -2,13 +2,20 @@
 
 ## v0.3.0
 
+__New features__:
+
 * [#44: Rewrite entirely using Deferred](https://github.com/nghuuphuoc/bootstrapvalidator/issues/44)
+* [#64: Support Danish zip code](https://github.com/nghuuphuoc/bootstrapvalidator/issues/64)
 * [#71: Show all errors](https://github.com/nghuuphuoc/bootstrapvalidator/issues/71)
+
+__Fixes__:
+
 * [#51: Submit after submit doesn't work](https://github.com/nghuuphuoc/bootstrapvalidator/issues/51)
 * [#53: Fix notEmpty validator for radios and checkboxes](https://github.com/nghuuphuoc/bootstrapvalidator/issues/53)
 * [#62: The callback validator passes wrong parameter](https://github.com/nghuuphuoc/bootstrapvalidator/issues/62)
 
-Document:
+__Document__:
+
 * [#60: Update the installation guide](https://github.com/nghuuphuoc/bootstrapvalidator/pull/60)
 * [#73: Describe which version should be included in the Usage section](https://github.com/nghuuphuoc/bootstrapvalidator/issues/73)
 

+ 4 - 3
Gruntfile.js

@@ -6,12 +6,13 @@ module.exports = function(grunt) {
 
         banner: [
             '/**',
-            ' * BootstrapValidator v<%= pkg.version %> (<%= pkg.homepage %>)',
+            ' * BootstrapValidator (<%= pkg.homepage %>)',
             ' *',
             ' * A jQuery plugin to validate form fields. Use with Bootstrap 3',
             ' *',
-            ' * @author      Nguyen Huu Phuoc <phuoc@huuphuoc.me>',
-            ' * @copyright   (c) 2013 Nguyen Huu Phuoc',
+            ' * @version     v<%= pkg.version %>',
+            ' * @author      <%= pkg.author.url %>',
+            ' * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc',
             ' * @license     MIT',
             ' */\n\n'
         ].join('\n'),

+ 11 - 2
README.md

@@ -131,7 +131,7 @@ notEmpty                                | Check if the value is empty
 [remote](#remote-validator)             | Perform remote checking via Ajax request
 [stringLength](#stringlength-validator) | Validate the length of a string
 uri                                     | Validate an URL address
-usZipCode                               | Validate a US zip code
+[zipCode](#zipcode-validator)           | Validate a zip code
 
 The validator options are described in the following section:
 
@@ -221,6 +221,15 @@ message     | n/a     | The error message
 min         | n/a     | The minimum length
 max         | n/a     | The maximum length. One of ```min```, ```max``` options is required
 
+### ZipCode Validator
+
+| Option name | Default | Description
+| ------------|---------|------------
+| message     | n/a     | The error message
+| country     | US      | A ISO 3166 country code. Currently it supports the following countries:
+|             |         | - US (United State)
+|             |         | - DK (Denmark)
+
 ## Write new validator
 
 A validator has to follow the syntax:
@@ -310,7 +319,7 @@ This software is written by Nguyen Huu Phuoc, aka @nghuuphuoc
 
 Big thanks to the contributor:
 
-* Vu Minh Khang, aka [@khangvm53](https://github.com/khangvm53)
+[@khangvm53](https://github.com/khangvm53)
 
 ## License
 

+ 1 - 1
demo/remote.php

@@ -4,7 +4,7 @@
 // and open the remote.html file:
 // http://domain.com/demo/remote.html
 
-sleep(5);
+//sleep(5);
 
 $valid = true;
 

+ 2 - 1
demo/validators.html

@@ -205,7 +205,8 @@ $(document).ready(function() {
             },
             zipCode: {
                 validators: {
-                    usZipCode: {
+                    zipCode: {
+                        country: 'US',
                         message: 'The input is not a valid US zip code'
                     }
                 }

+ 17 - 5
dist/js/bootstrapValidator.js

@@ -826,21 +826,33 @@
     };
 }(window.jQuery));
 ;(function($) {
-    $.fn.bootstrapValidator.validators.usZipCode = {
+    $.fn.bootstrapValidator.validators.zipCode = {
         /**
-         * Return true if and only if the input value is a valid US zip code
+         * Return true if and only if the input value is a valid country zip code
          *
          * @param {BootstrapValidator} validator The validator plugin instance
          * @param {jQuery} $field Field element
-         * @param {Object} options
+         * @param {Object} options Consist of key:
+         * - country: The ISO 3166 country code
+         *
+         * Currently it supports the following countries:
+         * - US (United State)
+         * - DK (Denmark)
+         *
          * @returns {Boolean}
          */
         validate: function(validateInstance, $field, options) {
             var value = $field.val();
-            if (value == '') {
+            if (value == '' || !options.country) {
                 return true;
             }
-            return /^\d{5}([\-]\d{4})?$/.test(value);
+            switch (options.country.toUpperCase()) {
+                case 'DK':
+                    return /^(DK(-|\s)?)?\d{4}$/i.test(value);
+                case 'US':
+                default:
+                    return /^\d{5}([\-]\d{4})?$/.test(value);
+            }
         }
     };
 }(window.jQuery));

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


+ 6 - 6
package.json

@@ -6,12 +6,12 @@
   },
   "homepage": "http://github.com/nghuuphuoc/bootstrapvalidator",
   "devDependencies": {
-    "grunt": "~0.4.1",
-    "grunt-contrib-uglify": "~0.2.4",
+    "grunt": "~0.4.2",
     "grunt-contrib-concat": "~0.3.0",
-    "grunt-contrib-watch": "~0.5.3",
-    "grunt-contrib-cssmin": "~0.6.2",
-    "grunt-contrib-copy": "~0.4.1"
+    "grunt-contrib-copy": "~0.5.0",
+    "grunt-contrib-cssmin": "~0.9.0",
+    "grunt-contrib-uglify": "~0.4.0",
+    "grunt-contrib-watch": "~0.5.3"
   },
-  "version": "0.2.3"
+  "version": "0.3.0"
 }

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

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

+ 31 - 0
src/js/validator/zipCode.js

@@ -0,0 +1,31 @@
+(function($) {
+    $.fn.bootstrapValidator.validators.zipCode = {
+        /**
+         * Return true if and only if the input value is a valid country zip code
+         *
+         * @param {BootstrapValidator} validator The validator plugin instance
+         * @param {jQuery} $field Field element
+         * @param {Object} options Consist of key:
+         * - country: The ISO 3166 country code
+         *
+         * Currently it supports the following countries:
+         * - US (United State)
+         * - DK (Denmark)
+         *
+         * @returns {Boolean}
+         */
+        validate: function(validateInstance, $field, options) {
+            var value = $field.val();
+            if (value == '' || !options.country) {
+                return true;
+            }
+            switch (options.country.toUpperCase()) {
+                case 'DK':
+                    return /^(DK(-|\s)?)?\d{4}$/i.test(value);
+                case 'US':
+                default:
+                    return /^\d{5}([\-]\d{4})?$/.test(value);
+            }
+        }
+    };
+}(window.jQuery));