isPostalCode.js 2.4 KB

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