zipCode.js 8.3 KB

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