zipCode.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. (function($) {
  2. $.fn.bootstrapValidator.i18n.zipCode = $.extend($.fn.bootstrapValidator.i18n.zipCode || {}, {
  3. 'default': 'Please enter a valid postal code',
  4. countryNotSupported: 'The country code %s is not supported',
  5. country: 'Please enter a valid postal code in %s',
  6. countries: {
  7. BR: 'Brazil',
  8. CA: 'Canada',
  9. CZ: 'Czech Republic',
  10. DK: 'Denmark',
  11. EI: 'Ireland',
  12. GB: 'United Kingdom',
  13. IT: 'Italy',
  14. MA: 'Morocco',
  15. NL: 'Netherlands',
  16. RO: 'Romania',
  17. RU: 'Russia',
  18. SE: 'Sweden',
  19. SG: 'Singapore',
  20. SK: 'Slovakia',
  21. US: 'USA'
  22. }
  23. });
  24. $.fn.bootstrapValidator.validators.zipCode = {
  25. html5Attributes: {
  26. message: 'message',
  27. country: 'country'
  28. },
  29. COUNTRY_CODES: ['BR', 'CA', 'CZ', 'DK', 'EI', 'GB', 'IT', 'MA', 'NL', 'RO', 'RU', 'SE', 'SG', 'SK', 'US'],
  30. /**
  31. * Return true if and only if the input value is a valid country zip code
  32. *
  33. * @param {BootstrapValidator} validator The validator plugin instance
  34. * @param {jQuery} $field Field element
  35. * @param {Object} options Consist of key:
  36. * - message: The invalid message
  37. * - country: The country
  38. *
  39. * The country can be defined by:
  40. * - An ISO 3166 country code
  41. * - Name of field which its value defines the country code
  42. * - Name of callback function that returns the country code
  43. * - A callback function that returns the country code
  44. *
  45. * callback: function(value, validator, $field) {
  46. * // value is the value of field
  47. * // validator is the BootstrapValidator instance
  48. * // $field is jQuery element representing the field
  49. * }
  50. *
  51. * @returns {Boolean|Object}
  52. */
  53. validate: function(validator, $field, options) {
  54. var value = $field.val();
  55. if (value === '' || !options.country) {
  56. return true;
  57. }
  58. var country = options.country;
  59. if (typeof country !== 'string' || $.inArray(country, this.COUNTRY_CODES) === -1) {
  60. // Try to determine the country
  61. country = validator.getDynamicOption($field, country);
  62. }
  63. if (!country || $.inArray(country.toUpperCase(), this.COUNTRY_CODES) === -1) {
  64. return { valid: false, message: $.fn.bootstrapValidator.helpers.format($.fn.bootstrapValidator.i18n.zipCode.countryNotSupported, country) };
  65. }
  66. var isValid = false;
  67. country = country.toUpperCase();
  68. switch (country) {
  69. case 'BR':
  70. isValid = /^(\d{2})([\.]?)(\d{3})([\-]?)(\d{3})$/.test(value);
  71. break;
  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 'CZ':
  76. // Test: http://regexr.com/39hhr
  77. isValid = /^(\d{3})([ ]?)(\d{2})$/.test(value);
  78. break;
  79. case 'DK':
  80. isValid = /^(DK(-|\s)?)?\d{4}$/i.test(value);
  81. break;
  82. // http://www.eircode.ie/docs/default-source/Common/prepare-your-business-for-eircode---published-v2.pdf?sfvrsn=2
  83. // Test: http://refiddle.com/1kpl
  84. case 'EI':
  85. isValid = /^(D6W|[ACDEFHKNPRTVWXY]\d{2})\s[0-9ACDEFHKNPRTVWXY]{4}$/.test(value);
  86. break;
  87. case 'GB':
  88. isValid = this._gb(value);
  89. break;
  90. // http://en.wikipedia.org/wiki/List_of_postal_codes_in_Italy
  91. case 'IT':
  92. isValid = /^(I-|IT-)?\d{5}$/i.test(value);
  93. break;
  94. // http://en.wikipedia.org/wiki/List_of_postal_codes_in_Morocco
  95. case 'MA':
  96. isValid = /^[1-9][0-9]{4}$/i.test(value);
  97. break;
  98. // http://en.wikipedia.org/wiki/Postal_codes_in_the_Netherlands
  99. case 'NL':
  100. isValid = /^[1-9][0-9]{3} ?(?!sa|sd|ss)[a-z]{2}$/i.test(value);
  101. break;
  102. case 'RO':
  103. isValid = /^(0[1-8]{1}|[1-9]{1}[0-5]{1})?[0-9]{4}$/i.test(value);
  104. break;
  105. case 'RU':
  106. isValid = /^[0-9]{6}$/i.test(value);
  107. break;
  108. case 'SE':
  109. isValid = /^(S-)?\d{3}\s?\d{2}$/i.test(value);
  110. break;
  111. case 'SG':
  112. isValid = /^([0][1-9]|[1-6][0-9]|[7]([0-3]|[5-9])|[8][0-2])(\d{4})$/i.test(value);
  113. break;
  114. case 'SK':
  115. // Test: http://regexr.com/39hhr
  116. isValid = /^(\d{3})([ ]?)(\d{2})$/.test(value);
  117. break;
  118. case 'US':
  119. /* falls through */
  120. default:
  121. isValid = /^\d{4,5}([\-]?\d{4})?$/.test(value);
  122. break;
  123. }
  124. return {
  125. valid: isValid,
  126. message: $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.zipCode.country, $.fn.bootstrapValidator.i18n.zipCode.countries[country])
  127. };
  128. },
  129. /**
  130. * Validate United Kingdom postcode
  131. * Examples:
  132. * - Standard: EC1A 1BB, W1A 1HQ, M1 1AA, B33 8TH, CR2 6XH, DN55 1PT
  133. * - Special cases:
  134. * AI-2640, ASCN 1ZZ, GIR 0AA
  135. *
  136. * @see http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
  137. * @param {String} value The postcode
  138. * @returns {Boolean}
  139. */
  140. _gb: function(value) {
  141. var firstChar = '[ABCDEFGHIJKLMNOPRSTUWYZ]', // Does not accept QVX
  142. secondChar = '[ABCDEFGHKLMNOPQRSTUVWXY]', // Does not accept IJZ
  143. thirdChar = '[ABCDEFGHJKPMNRSTUVWXY]',
  144. fourthChar = '[ABEHMNPRVWXY]',
  145. fifthChar = '[ABDEFGHJLNPQRSTUWXYZ]',
  146. regexps = [
  147. // AN NAA, ANN NAA, AAN NAA, AANN NAA format
  148. new RegExp('^(' + firstChar + '{1}' + secondChar + '?[0-9]{1,2})(\\s*)([0-9]{1}' + fifthChar + '{2})$', 'i'),
  149. // ANA NAA
  150. new RegExp('^(' + firstChar + '{1}[0-9]{1}' + thirdChar + '{1})(\\s*)([0-9]{1}' + fifthChar + '{2})$', 'i'),
  151. // AANA NAA
  152. new RegExp('^(' + firstChar + '{1}' + secondChar + '{1}?[0-9]{1}' + fourthChar + '{1})(\\s*)([0-9]{1}' + fifthChar + '{2})$', 'i'),
  153. new RegExp('^(BF1)(\\s*)([0-6]{1}[ABDEFGHJLNPQRST]{1}[ABDEFGHJLNPQRSTUWZYZ]{1})$', 'i'), // BFPO postcodes
  154. /^(GIR)(\s*)(0AA)$/i, // Special postcode GIR 0AA
  155. /^(BFPO)(\s*)([0-9]{1,4})$/i, // Standard BFPO numbers
  156. /^(BFPO)(\s*)(c\/o\s*[0-9]{1,3})$/i, // c/o BFPO numbers
  157. /^([A-Z]{4})(\s*)(1ZZ)$/i, // Overseas Territories
  158. /^(AI-2640)$/i // Anguilla
  159. ];
  160. for (var i = 0; i < regexps.length; i++) {
  161. if (regexps[i].test(value)) {
  162. return true;
  163. }
  164. }
  165. return false;
  166. }
  167. };
  168. }(window.jQuery));