isPostalCode.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import assertString from './util/assertString';
  2. // common patterns
  3. var threeDigit = /^\d{3}$/;
  4. var fourDigit = /^\d{4}$/;
  5. var fiveDigit = /^\d{5}$/;
  6. var sixDigit = /^\d{6}$/;
  7. var patterns = {
  8. AD: /^AD\d{3}$/,
  9. AT: fourDigit,
  10. AU: fourDigit,
  11. AZ: /^AZ\d{4}$/,
  12. BA: /^([7-8]\d{4}$)/,
  13. BD: /^([1-8][0-9]{3}|9[0-4][0-9]{2})$/,
  14. BE: fourDigit,
  15. BG: fourDigit,
  16. BR: /^\d{5}-?\d{3}$/,
  17. BY: /^2[1-4]\d{4}$/,
  18. CA: /^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJ-NPRSTV-Z][\s\-]?\d[ABCEGHJ-NPRSTV-Z]\d$/i,
  19. CH: fourDigit,
  20. CN: /^(0[1-7]|1[012356]|2[0-7]|3[0-6]|4[0-7]|5[1-7]|6[1-7]|7[1-5]|8[1345]|9[09])\d{4}$/,
  21. CO: /^(05|08|11|13|15|17|18|19|20|23|25|27|41|44|47|50|52|54|63|66|68|70|73|76|81|85|86|88|91|94|95|97|99)(\d{4})$/,
  22. CZ: /^\d{3}\s?\d{2}$/,
  23. DE: fiveDigit,
  24. DK: fourDigit,
  25. DO: fiveDigit,
  26. DZ: fiveDigit,
  27. EE: fiveDigit,
  28. ES: /^(5[0-2]{1}|[0-4]{1}\d{1})\d{3}$/,
  29. FI: fiveDigit,
  30. FR: /^(?:(?:0[1-9]|[1-8]\d|9[0-5])\d{3}|97[1-46]\d{2})$/,
  31. GB: /^(gir\s?0aa|[a-z]{1,2}\d[\da-z]?\s?(\d[a-z]{2})?)$/i,
  32. GR: /^\d{3}\s?\d{2}$/,
  33. HR: /^([1-5]\d{4}$)/,
  34. HT: /^HT\d{4}$/,
  35. HU: fourDigit,
  36. ID: fiveDigit,
  37. IE: /^(?!.*(?:o))[A-Za-z]\d[\dw]\s\w{4}$/i,
  38. IL: /^(\d{5}|\d{7})$/,
  39. IN: /^((?!10|29|35|54|55|65|66|86|87|88|89)[1-9][0-9]{5})$/,
  40. IR: /^(?!(\d)\1{3})[13-9]{4}[1346-9][013-9]{5}$/,
  41. IS: threeDigit,
  42. IT: fiveDigit,
  43. JP: /^\d{3}\-\d{4}$/,
  44. KE: fiveDigit,
  45. KR: /^(\d{5}|\d{6})$/,
  46. LI: /^(948[5-9]|949[0-7])$/,
  47. LT: /^LT\-\d{5}$/,
  48. LU: fourDigit,
  49. LV: /^LV\-\d{4}$/,
  50. LK: fiveDigit,
  51. MG: threeDigit,
  52. MX: fiveDigit,
  53. MT: /^[A-Za-z]{3}\s{0,1}\d{4}$/,
  54. MY: fiveDigit,
  55. NL: /^[1-9]\d{3}\s?(?!sa|sd|ss)[a-z]{2}$/i,
  56. NO: fourDigit,
  57. NP: /^(10|21|22|32|33|34|44|45|56|57)\d{3}$|^(977)$/i,
  58. NZ: fourDigit,
  59. // https://www.pakpost.gov.pk/postcodes.php
  60. PK: fiveDigit,
  61. PL: /^\d{2}\-\d{3}$/,
  62. PR: /^00[679]\d{2}([ -]\d{4})?$/,
  63. PT: /^\d{4}\-\d{3}?$/,
  64. RO: sixDigit,
  65. RU: sixDigit,
  66. SA: fiveDigit,
  67. SE: /^[1-9]\d{2}\s?\d{2}$/,
  68. SG: sixDigit,
  69. SI: fourDigit,
  70. SK: /^\d{3}\s?\d{2}$/,
  71. TH: fiveDigit,
  72. TN: fourDigit,
  73. TW: /^\d{3}(\d{2,3})?$/,
  74. UA: fiveDigit,
  75. US: /^\d{5}(-\d{4})?$/,
  76. ZA: fourDigit,
  77. ZM: fiveDigit
  78. };
  79. export var locales = Object.keys(patterns);
  80. export default function isPostalCode(str, locale) {
  81. assertString(str);
  82. if (locale in patterns) {
  83. return patterns[locale].test(str);
  84. } else if (locale === 'any') {
  85. for (var key in patterns) {
  86. // https://github.com/gotwarlost/istanbul/blob/master/ignoring-code-for-coverage.md#ignoring-code-for-coverage-purposes
  87. // istanbul ignore else
  88. if (patterns.hasOwnProperty(key)) {
  89. var pattern = patterns[key];
  90. if (pattern.test(str)) {
  91. return true;
  92. }
  93. }
  94. }
  95. return false;
  96. }
  97. throw new Error("Invalid locale '".concat(locale, "'"));
  98. }