Browse Source

#220: Add UK postcode support for the zipCode validator

phuoc 11 years ago
parent
commit
010ceea25f
4 changed files with 90 additions and 1 deletions
  1. 1 0
      CHANGELOG.md
  2. 44 0
      dist/js/bootstrapValidator.js
  3. 1 1
      dist/js/bootstrapValidator.min.js
  4. 44 0
      src/js/validator/zipCode.js

+ 1 - 0
CHANGELOG.md

@@ -10,6 +10,7 @@
 * [#216](https://github.com/nghuuphuoc/bootstrapvalidator/issues/216): Add ISMN (International Standard Music Number) validator
 * [#217](https://github.com/nghuuphuoc/bootstrapvalidator/issues/217): Add ISSN (International Standard Serial Number) validator
 * [#191](https://github.com/nghuuphuoc/bootstrapvalidator/issues/191), [#223](https://github.com/nghuuphuoc/bootstrapvalidator/issues/223): Support using both the ```name``` attribute and ```selector``` option for field
+* [#220](https://github.com/nghuuphuoc/bootstrapvalidator/issues/220): Add UK postcode support for the [```zipCode``` validator](http://bootstrapvalidator.com/validators/zip-code/)
 * [#229](https://github.com/nghuuphuoc/bootstrapvalidator/issues/229): The [```date``` validator](http://bootstrapvalidator.com/validators/date/) supports seconds
 
 ## v0.4.3 (2014-04-26)

+ 44 - 0
dist/js/bootstrapValidator.js

@@ -3857,6 +3857,7 @@
          * - US (United State)
          * - CA (Canada)
          * - DK (Denmark)
+         * - GB (United Kingdom)
          * - SE (Sweden)
          * @returns {Boolean}
          */
@@ -3872,12 +3873,55 @@
                     return /(?:A|B|C|E|G|J|K|L|M|N|P|R|S|T|V|X|Y){1}[0-9]{1}(?:A|B|C|E|G|J|K|L|M|N|P|R|S|T|V|X|Y){1}\s?[0-9]{1}(?:A|B|C|E|G|J|K|L|M|N|P|R|S|T|V|X|Y){1}[0-9]{1}/i.test(value);
                 case 'DK':
                     return /^(DK(-|\s)?)?\d{4}$/i.test(value);
+                case 'GB':
+                    return this._gb(value);
                 case 'SE':
                     return /^(S-)?\d{3}\s?\d{2}$/i.test(value);
                 case 'US':
                 default:
                     return /^\d{4,5}([\-]\d{4})?$/.test(value);
             }
+        },
+
+        /**
+         * Validate United Kingdom postcode
+         * Examples:
+         * - Standard: EC1A 1BB, W1A 1HQ, M1 1AA, B33 8TH, CR2 6XH, DN55 1PT
+         * - Special cases:
+         * AI-2640, ASCN 1ZZ, GIR 0AA
+         *
+         * @see http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
+         * @param {String} value The postcode
+         * @returns {Boolean}
+         */
+        _gb: function(value) {
+            var firstChar  = '[ABCDEFGHIJKLMNOPRSTUWYZ]',     // Does not accept QVX
+                secondChar = '[ABCDEFGHKLMNOPQRSTUVWXY]',     // Does not accept IJZ
+                thirdChar  = '[ABCDEFGHJKPMNRSTUVWXY]',
+                fouthChar  = '[ABEHMNPRVWXY]',
+                fifthChar  = '[ABDEFGHJLNPQRSTUWXYZ]',
+                regexps = [
+                    // AN NAA, ANN NAA, AAN NAA, AANN NAA format
+                    new RegExp('^(' + firstChar + '{1}' + secondChar + '?[0-9]{1,2})(\\s*)([0-9]{1}' + fifthChar + '{2})$', 'i'),
+                    // ANA NAA
+                    new RegExp('^(' + firstChar + '{1}[0-9]{1}' + thirdChar + '{1})(\\s*)([0-9]{1}' + fifthChar + '{2})$', 'i'),
+                    // AANA NAA
+                    new RegExp('^(' + firstChar + '{1}' + secondChar + '{1}?[0-9]{1}' + fouthChar + '{1})(\\s*)([0-9]{1}' + fifthChar + '{2})$', 'i'),
+
+                    new RegExp('^(BF1)(\\s*)([0-6]{1}[ABDEFGHJLNPQRST]{1}[ABDEFGHJLNPQRSTUWZYZ]{1})$', 'i'),        // BFPO postcodes
+                    /^(GIR)(\s*)(0AA)$/i,                       // Special postcode GIR 0AA
+                    /^(BFPO)(\s*)([0-9]{1,4})$/i,               // Standard BFPO numbers
+                    /^(BFPO)(\s*)(c\/o\s*[0-9]{1,3})$/i,        // c/o BFPO numbers
+                    /^([A-Z]{4})(\s*)(1ZZ)$/i,                  // Overseas Territories
+                    /^(AI-2640)$/i                              // Anguilla
+                ];
+            for (var i = 0; i < regexps.length; i++) {
+                if (regexps[i].test(value)) {
+                    return true;
+                }
+            }
+
+            return false;
         }
     };
 }(window.jQuery));

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


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

@@ -18,6 +18,7 @@
          * - US (United State)
          * - CA (Canada)
          * - DK (Denmark)
+         * - GB (United Kingdom)
          * - SE (Sweden)
          * @returns {Boolean}
          */
@@ -33,12 +34,55 @@
                     return /(?:A|B|C|E|G|J|K|L|M|N|P|R|S|T|V|X|Y){1}[0-9]{1}(?:A|B|C|E|G|J|K|L|M|N|P|R|S|T|V|X|Y){1}\s?[0-9]{1}(?:A|B|C|E|G|J|K|L|M|N|P|R|S|T|V|X|Y){1}[0-9]{1}/i.test(value);
                 case 'DK':
                     return /^(DK(-|\s)?)?\d{4}$/i.test(value);
+                case 'GB':
+                    return this._gb(value);
                 case 'SE':
                     return /^(S-)?\d{3}\s?\d{2}$/i.test(value);
                 case 'US':
                 default:
                     return /^\d{4,5}([\-]\d{4})?$/.test(value);
             }
+        },
+
+        /**
+         * Validate United Kingdom postcode
+         * Examples:
+         * - Standard: EC1A 1BB, W1A 1HQ, M1 1AA, B33 8TH, CR2 6XH, DN55 1PT
+         * - Special cases:
+         * AI-2640, ASCN 1ZZ, GIR 0AA
+         *
+         * @see http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
+         * @param {String} value The postcode
+         * @returns {Boolean}
+         */
+        _gb: function(value) {
+            var firstChar  = '[ABCDEFGHIJKLMNOPRSTUWYZ]',     // Does not accept QVX
+                secondChar = '[ABCDEFGHKLMNOPQRSTUVWXY]',     // Does not accept IJZ
+                thirdChar  = '[ABCDEFGHJKPMNRSTUVWXY]',
+                fouthChar  = '[ABEHMNPRVWXY]',
+                fifthChar  = '[ABDEFGHJLNPQRSTUWXYZ]',
+                regexps = [
+                    // AN NAA, ANN NAA, AAN NAA, AANN NAA format
+                    new RegExp('^(' + firstChar + '{1}' + secondChar + '?[0-9]{1,2})(\\s*)([0-9]{1}' + fifthChar + '{2})$', 'i'),
+                    // ANA NAA
+                    new RegExp('^(' + firstChar + '{1}[0-9]{1}' + thirdChar + '{1})(\\s*)([0-9]{1}' + fifthChar + '{2})$', 'i'),
+                    // AANA NAA
+                    new RegExp('^(' + firstChar + '{1}' + secondChar + '{1}?[0-9]{1}' + fouthChar + '{1})(\\s*)([0-9]{1}' + fifthChar + '{2})$', 'i'),
+
+                    new RegExp('^(BF1)(\\s*)([0-6]{1}[ABDEFGHJLNPQRST]{1}[ABDEFGHJLNPQRSTUWZYZ]{1})$', 'i'),        // BFPO postcodes
+                    /^(GIR)(\s*)(0AA)$/i,                       // Special postcode GIR 0AA
+                    /^(BFPO)(\s*)([0-9]{1,4})$/i,               // Standard BFPO numbers
+                    /^(BFPO)(\s*)(c\/o\s*[0-9]{1,3})$/i,        // c/o BFPO numbers
+                    /^([A-Z]{4})(\s*)(1ZZ)$/i,                  // Overseas Territories
+                    /^(AI-2640)$/i                              // Anguilla
+                ];
+            for (var i = 0; i < regexps.length; i++) {
+                if (regexps[i].test(value)) {
+                    return true;
+                }
+            }
+
+            return false;
         }
     };
 }(window.jQuery));