zipCode.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. (function($) {
  2. $.fn.bootstrapValidator.i18n.zipCode = $.extend($.fn.bootstrapValidator.i18n.zipCode || {}, {
  3. 'default': 'Please enter a valid zip code',
  4. countryNotSupported: 'The country code %s is not supported',
  5. country: 'Please enter a valid %s',
  6. countries: {
  7. 'CA': 'Canadian postal code',
  8. 'DK': 'Danish postal code',
  9. 'GB': 'United Kingdom postal code',
  10. 'IT': 'Italian postal code',
  11. 'NL': 'Dutch postal code',
  12. 'SE': 'Swiss postal code',
  13. 'SG': 'Singapore postal code',
  14. 'US': 'US zip code'
  15. }
  16. });
  17. $.fn.bootstrapValidator.validators.zipCode = {
  18. html5Attributes: {
  19. message: 'message',
  20. country: 'country'
  21. },
  22. COUNTRY_CODES: ['CA', 'DK', 'GB', 'IT', 'NL', 'SE', 'SG', 'US'],
  23. /**
  24. * Return true if and only if the input value is a valid country zip code
  25. *
  26. * @param {BootstrapValidator} validator The validator plugin instance
  27. * @param {jQuery} $field Field element
  28. * @param {Object} options Consist of key:
  29. * - message: The invalid message
  30. * - country: The country
  31. *
  32. * The country can be defined by:
  33. * - An ISO 3166 country code
  34. * Currently it supports the following countries:
  35. * - US (United States)
  36. * - CA (Canada)
  37. * - DK (Denmark)
  38. * - GB (United Kingdom)
  39. * - IT (Italy)
  40. * - NL (Netherlands)
  41. * - SE (Sweden)
  42. * - SG (Singapore)
  43. *
  44. * - Name of field which its value defines the country code
  45. * - Name of callback function that returns the country code
  46. * - A callback function that returns the country code
  47. *
  48. * callback: function(value, validator, $field) {
  49. * // value is the value of field
  50. * // validator is the BootstrapValidator instance
  51. * // $field is jQuery element representing the field
  52. * }
  53. *
  54. * @returns {Boolean|Object}
  55. */
  56. validate: function(validator, $field, options) {
  57. var value = $field.val();
  58. if (value === '' || !options.country) {
  59. return true;
  60. }
  61. var country = options.country;
  62. if (typeof country !== 'string' || $.inArray(country, this.COUNTRY_CODES) === -1) {
  63. // Try to determine the country
  64. country = validator.getDynamicOption($field, country);
  65. }
  66. if (!country || $.inArray(country.toUpperCase(), this.COUNTRY_CODES) === -1) {
  67. return { valid: false, message: $.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.zipCode.countryNotSupported, country) };
  68. }
  69. var isValid = false;
  70. country = country.toUpperCase();
  71. switch (country) {
  72. case 'CA':
  73. isValid = /^(?: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|W|X|Y|Z){1}\s?[0-9]{1}(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|W|X|Y|Z){1}[0-9]{1}$/i.test(value);
  74. break;
  75. case 'DK':
  76. isValid = /^(DK(-|\s)?)?\d{4}$/i.test(value);
  77. break;
  78. case 'GB':
  79. isValid = this._gb(value);
  80. break;
  81. // http://en.wikipedia.org/wiki/List_of_postal_codes_in_Italy
  82. case 'IT':
  83. isValid = /^(I-|IT-)?\d{5}$/i.test(value);
  84. break;
  85. // http://en.wikipedia.org/wiki/Postal_codes_in_the_Netherlands
  86. case 'NL':
  87. isValid = /^[1-9][0-9]{3} ?(?!sa|sd|ss)[a-z]{2}$/i.test(value);
  88. break;
  89. case 'SE':
  90. isValid = /^(S-)?\d{3}\s?\d{2}$/i.test(value);
  91. break;
  92. case 'SG':
  93. isValid = /^([0][1-9]|[1-6][0-9]|[7]([0-3]|[5-9])|[8][0-2])(\d{4})$/i.test(value);
  94. break;
  95. case 'US':
  96. /* falls through */
  97. default:
  98. isValid = /^\d{4,5}([\-]?\d{4})?$/.test(value);
  99. break;
  100. }
  101. return {
  102. valid: isValid,
  103. message: $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.zipCode.country, $.fn.bootstrapValidator.i18n.zipCode.countries[country])
  104. };
  105. },
  106. /**
  107. * Validate United Kingdom postcode
  108. * Examples:
  109. * - Standard: EC1A 1BB, W1A 1HQ, M1 1AA, B33 8TH, CR2 6XH, DN55 1PT
  110. * - Special cases:
  111. * AI-2640, ASCN 1ZZ, GIR 0AA
  112. *
  113. * @see http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
  114. * @param {String} value The postcode
  115. * @returns {Boolean}
  116. */
  117. _gb: function(value) {
  118. var firstChar = '[ABCDEFGHIJKLMNOPRSTUWYZ]', // Does not accept QVX
  119. secondChar = '[ABCDEFGHKLMNOPQRSTUVWXY]', // Does not accept IJZ
  120. thirdChar = '[ABCDEFGHJKPMNRSTUVWXY]',
  121. fourthChar = '[ABEHMNPRVWXY]',
  122. fifthChar = '[ABDEFGHJLNPQRSTUWXYZ]',
  123. regexps = [
  124. // AN NAA, ANN NAA, AAN NAA, AANN NAA format
  125. new RegExp('^(' + firstChar + '{1}' + secondChar + '?[0-9]{1,2})(\\s*)([0-9]{1}' + fifthChar + '{2})$', 'i'),
  126. // ANA NAA
  127. new RegExp('^(' + firstChar + '{1}[0-9]{1}' + thirdChar + '{1})(\\s*)([0-9]{1}' + fifthChar + '{2})$', 'i'),
  128. // AANA NAA
  129. new RegExp('^(' + firstChar + '{1}' + secondChar + '{1}?[0-9]{1}' + fourthChar + '{1})(\\s*)([0-9]{1}' + fifthChar + '{2})$', 'i'),
  130. new RegExp('^(BF1)(\\s*)([0-6]{1}[ABDEFGHJLNPQRST]{1}[ABDEFGHJLNPQRSTUWZYZ]{1})$', 'i'), // BFPO postcodes
  131. /^(GIR)(\s*)(0AA)$/i, // Special postcode GIR 0AA
  132. /^(BFPO)(\s*)([0-9]{1,4})$/i, // Standard BFPO numbers
  133. /^(BFPO)(\s*)(c\/o\s*[0-9]{1,3})$/i, // c/o BFPO numbers
  134. /^([A-Z]{4})(\s*)(1ZZ)$/i, // Overseas Territories
  135. /^(AI-2640)$/i // Anguilla
  136. ];
  137. for (var i = 0; i < regexps.length; i++) {
  138. if (regexps[i].test(value)) {
  139. return true;
  140. }
  141. }
  142. return false;
  143. }
  144. };
  145. }(window.jQuery));