zipCode.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. FR: 'France',
  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', 'FR', '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://en.wikipedia.org/wiki/Postal_codes_in_France
  83. case 'FR':
  84. isValid = /^[0-9]{5}$/i.test(value);
  85. break;
  86. case 'GB':
  87. isValid = this._gb(value);
  88. break;
  89. // http://en.wikipedia.org/wiki/List_of_postal_codes_in_Italy
  90. case 'IT':
  91. isValid = /^(I-|IT-)?\d{5}$/i.test(value);
  92. break;
  93. // http://en.wikipedia.org/wiki/List_of_postal_codes_in_Morocco
  94. case 'MA':
  95. isValid = /^[1-9][0-9]{4}$/i.test(value);
  96. break;
  97. // http://en.wikipedia.org/wiki/Postal_codes_in_the_Netherlands
  98. case 'NL':
  99. isValid = /^[1-9][0-9]{3} ?(?!sa|sd|ss)[a-z]{2}$/i.test(value);
  100. break;
  101. case 'RO':
  102. isValid = /^(0[1-8]{1}|[1-9]{1}[0-5]{1})?[0-9]{4}$/i.test(value);
  103. break;
  104. case 'RU':
  105. isValid = /^[0-9]{6}$/i.test(value);
  106. break;
  107. case 'SE':
  108. isValid = /^(S-)?\d{3}\s?\d{2}$/i.test(value);
  109. break;
  110. case 'SG':
  111. isValid = /^([0][1-9]|[1-6][0-9]|[7]([0-3]|[5-9])|[8][0-2])(\d{4})$/i.test(value);
  112. break;
  113. case 'SK':
  114. // Test: http://regexr.com/39hhr
  115. isValid = /^(\d{3})([ ]?)(\d{2})$/.test(value);
  116. break;
  117. case 'US':
  118. /* falls through */
  119. default:
  120. isValid = /^\d{4,5}([\-]?\d{4})?$/.test(value);
  121. break;
  122. }
  123. return {
  124. valid: isValid,
  125. message: $.fn.bootstrapValidator.helpers.format(options.message || $.fn.bootstrapValidator.i18n.zipCode.country, $.fn.bootstrapValidator.i18n.zipCode.countries[country])
  126. };
  127. },
  128. /**
  129. * Validate United Kingdom postcode
  130. * Examples:
  131. * - Standard: EC1A 1BB, W1A 1HQ, M1 1AA, B33 8TH, CR2 6XH, DN55 1PT
  132. * - Special cases:
  133. * AI-2640, ASCN 1ZZ, GIR 0AA
  134. *
  135. * @see http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
  136. * @param {String} value The postcode
  137. * @returns {Boolean}
  138. */
  139. _gb: function(value) {
  140. var firstChar = '[ABCDEFGHIJKLMNOPRSTUWYZ]', // Does not accept QVX
  141. secondChar = '[ABCDEFGHKLMNOPQRSTUVWXY]', // Does not accept IJZ
  142. thirdChar = '[ABCDEFGHJKPMNRSTUVWXY]',
  143. fourthChar = '[ABEHMNPRVWXY]',
  144. fifthChar = '[ABDEFGHJLNPQRSTUWXYZ]',
  145. regexps = [
  146. // AN NAA, ANN NAA, AAN NAA, AANN NAA format
  147. new RegExp('^(' + firstChar + '{1}' + secondChar + '?[0-9]{1,2})(\\s*)([0-9]{1}' + fifthChar + '{2})$', 'i'),
  148. // ANA NAA
  149. new RegExp('^(' + firstChar + '{1}[0-9]{1}' + thirdChar + '{1})(\\s*)([0-9]{1}' + fifthChar + '{2})$', 'i'),
  150. // AANA NAA
  151. new RegExp('^(' + firstChar + '{1}' + secondChar + '{1}?[0-9]{1}' + fourthChar + '{1})(\\s*)([0-9]{1}' + fifthChar + '{2})$', 'i'),
  152. new RegExp('^(BF1)(\\s*)([0-6]{1}[ABDEFGHJLNPQRST]{1}[ABDEFGHJLNPQRSTUWZYZ]{1})$', 'i'), // BFPO postcodes
  153. /^(GIR)(\s*)(0AA)$/i, // Special postcode GIR 0AA
  154. /^(BFPO)(\s*)([0-9]{1,4})$/i, // Standard BFPO numbers
  155. /^(BFPO)(\s*)(c\/o\s*[0-9]{1,3})$/i, // c/o BFPO numbers
  156. /^([A-Z]{4})(\s*)(1ZZ)$/i, // Overseas Territories
  157. /^(AI-2640)$/i // Anguilla
  158. ];
  159. for (var i = 0; i < regexps.length; i++) {
  160. if (regexps[i].test(value)) {
  161. return true;
  162. }
  163. }
  164. return false;
  165. }
  166. };
  167. }(window.jQuery));