isPostalCode.js 2.5 KB

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