zipCode.js 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. (function($) {
  2. $.fn.bootstrapValidator.validators.zipCode = {
  3. html5Attributes: {
  4. message: 'message',
  5. country: 'country'
  6. },
  7. /**
  8. * Return true if and only if the input value is a valid country zip code
  9. *
  10. * @param {BootstrapValidator} validator The validator plugin instance
  11. * @param {jQuery} $field Field element
  12. * @param {Object} options Consist of key:
  13. * - message: The invalid message
  14. * - country: The ISO 3166 country code
  15. *
  16. * Currently it supports the following countries:
  17. * - US (United State)
  18. * - CA (Canada)
  19. * - DK (Denmark)
  20. * - GB (United Kingdom)
  21. * - IT (Italy)
  22. * - NL (Netherlands)
  23. * - SE (Sweden)
  24. * @returns {Boolean}
  25. */
  26. validate: function(validator, $field, options) {
  27. var value = $field.val();
  28. if (value == '' || !options.country) {
  29. return true;
  30. }
  31. var country = (options.country || 'US').toUpperCase();
  32. switch (country) {
  33. case 'CA': return /^(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|X|Y){1}[0-9]{1}(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|X|Y){1}\s?[0-9]{1}(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|X|Y){1}[0-9]{1}$/i.test(value);
  34. case 'DK': return /^(DK(-|\s)?)?\d{4}$/i.test(value);
  35. case 'GB': return this._gb(value);
  36. // http://en.wikipedia.org/wiki/List_of_postal_codes_in_Italy
  37. case 'IT': return /^(I-|IT-)?\d{5}$/i.test(value);
  38. // http://en.wikipedia.org/wiki/Postal_codes_in_the_Netherlands
  39. case 'NL': return /^[1-9][0-9]{3} ?(?!sa|sd|ss)[a-z]{2}$/i.test(value);
  40. case 'SE': return /^(S-)?\d{3}\s?\d{2}$/i.test(value);
  41. case 'US':
  42. default: return /^\d{4,5}([\-]\d{4})?$/.test(value);
  43. }
  44. },
  45. /**
  46. * Validate United Kingdom postcode
  47. * Examples:
  48. * - Standard: EC1A 1BB, W1A 1HQ, M1 1AA, B33 8TH, CR2 6XH, DN55 1PT
  49. * - Special cases:
  50. * AI-2640, ASCN 1ZZ, GIR 0AA
  51. *
  52. * @see http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
  53. * @param {String} value The postcode
  54. * @returns {Boolean}
  55. */
  56. _gb: function(value) {
  57. var firstChar = '[ABCDEFGHIJKLMNOPRSTUWYZ]', // Does not accept QVX
  58. secondChar = '[ABCDEFGHKLMNOPQRSTUVWXY]', // Does not accept IJZ
  59. thirdChar = '[ABCDEFGHJKPMNRSTUVWXY]',
  60. fourthChar = '[ABEHMNPRVWXY]',
  61. fifthChar = '[ABDEFGHJLNPQRSTUWXYZ]',
  62. regexps = [
  63. // AN NAA, ANN NAA, AAN NAA, AANN NAA format
  64. new RegExp('^(' + firstChar + '{1}' + secondChar + '?[0-9]{1,2})(\\s*)([0-9]{1}' + fifthChar + '{2})$', 'i'),
  65. // ANA NAA
  66. new RegExp('^(' + firstChar + '{1}[0-9]{1}' + thirdChar + '{1})(\\s*)([0-9]{1}' + fifthChar + '{2})$', 'i'),
  67. // AANA NAA
  68. new RegExp('^(' + firstChar + '{1}' + secondChar + '{1}?[0-9]{1}' + fourthChar + '{1})(\\s*)([0-9]{1}' + fifthChar + '{2})$', 'i'),
  69. new RegExp('^(BF1)(\\s*)([0-6]{1}[ABDEFGHJLNPQRST]{1}[ABDEFGHJLNPQRSTUWZYZ]{1})$', 'i'), // BFPO postcodes
  70. /^(GIR)(\s*)(0AA)$/i, // Special postcode GIR 0AA
  71. /^(BFPO)(\s*)([0-9]{1,4})$/i, // Standard BFPO numbers
  72. /^(BFPO)(\s*)(c\/o\s*[0-9]{1,3})$/i, // c/o BFPO numbers
  73. /^([A-Z]{4})(\s*)(1ZZ)$/i, // Overseas Territories
  74. /^(AI-2640)$/i // Anguilla
  75. ];
  76. for (var i = 0; i < regexps.length; i++) {
  77. if (regexps[i].test(value)) {
  78. return true;
  79. }
  80. }
  81. return false;
  82. }
  83. };
  84. }(window.jQuery));