isPostalCode.js 2.5 KB

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