isPassportNumber.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import assertString from './util/assertString';
  2. /**
  3. * Reference:
  4. * https://en.wikipedia.org/ -- Wikipedia
  5. * https://docs.microsoft.com/en-us/microsoft-365/compliance/eu-passport-number -- EU Passport Number
  6. * https://countrycode.org/ -- Country Codes
  7. */
  8. var passportRegexByCountryCode = {
  9. AM: /^[A-Z]{2}\d{7}$/,
  10. // ARMENIA
  11. AR: /^[A-Z]{3}\d{6}$/,
  12. // ARGENTINA
  13. AT: /^[A-Z]\d{7}$/,
  14. // AUSTRIA
  15. AU: /^[A-Z]\d{7}$/,
  16. // AUSTRALIA
  17. BE: /^[A-Z]{2}\d{6}$/,
  18. // BELGIUM
  19. BG: /^\d{9}$/,
  20. // BULGARIA
  21. BR: /^[A-Z]{2}\d{6}$/,
  22. // BRAZIL
  23. BY: /^[A-Z]{2}\d{7}$/,
  24. // BELARUS
  25. CA: /^[A-Z]{2}\d{6}$/,
  26. // CANADA
  27. CH: /^[A-Z]\d{7}$/,
  28. // SWITZERLAND
  29. CN: /^[GE]\d{8}$/,
  30. // CHINA [G=Ordinary, E=Electronic] followed by 8-digits
  31. CY: /^[A-Z](\d{6}|\d{8})$/,
  32. // CYPRUS
  33. CZ: /^\d{8}$/,
  34. // CZECH REPUBLIC
  35. DE: /^[CFGHJKLMNPRTVWXYZ0-9]{9}$/,
  36. // GERMANY
  37. DK: /^\d{9}$/,
  38. // DENMARK
  39. DZ: /^\d{9}$/,
  40. // ALGERIA
  41. EE: /^([A-Z]\d{7}|[A-Z]{2}\d{7})$/,
  42. // ESTONIA (K followed by 7-digits), e-passports have 2 UPPERCASE followed by 7 digits
  43. ES: /^[A-Z0-9]{2}([A-Z0-9]?)\d{6}$/,
  44. // SPAIN
  45. FI: /^[A-Z]{2}\d{7}$/,
  46. // FINLAND
  47. FR: /^\d{2}[A-Z]{2}\d{5}$/,
  48. // FRANCE
  49. GB: /^\d{9}$/,
  50. // UNITED KINGDOM
  51. GR: /^[A-Z]{2}\d{7}$/,
  52. // GREECE
  53. HR: /^\d{9}$/,
  54. // CROATIA
  55. HU: /^[A-Z]{2}(\d{6}|\d{7})$/,
  56. // HUNGARY
  57. IE: /^[A-Z0-9]{2}\d{7}$/,
  58. // IRELAND
  59. IN: /^[A-Z]{1}-?\d{7}$/,
  60. // INDIA
  61. IR: /^[A-Z]\d{8}$/,
  62. // IRAN
  63. IS: /^(A)\d{7}$/,
  64. // ICELAND
  65. IT: /^[A-Z0-9]{2}\d{7}$/,
  66. // ITALY
  67. JP: /^[A-Z]{2}\d{7}$/,
  68. // JAPAN
  69. KR: /^[MS]\d{8}$/,
  70. // SOUTH KOREA, REPUBLIC OF KOREA, [S=PS Passports, M=PM Passports]
  71. LT: /^[A-Z0-9]{8}$/,
  72. // LITHUANIA
  73. LU: /^[A-Z0-9]{8}$/,
  74. // LUXEMBURG
  75. LV: /^[A-Z0-9]{2}\d{7}$/,
  76. // LATVIA
  77. LY: /^[A-Z0-9]{8}$/,
  78. // LIBYA
  79. MT: /^\d{7}$/,
  80. // MALTA
  81. MZ: /^([A-Z]{2}\d{7})|(\d{2}[A-Z]{2}\d{5})$/,
  82. // MOZAMBIQUE
  83. MY: /^[AHK]\d{8}$/,
  84. // MALAYSIA
  85. NL: /^[A-Z]{2}[A-Z0-9]{6}\d$/,
  86. // NETHERLANDS
  87. PO: /^[A-Z]{2}\d{7}$/,
  88. // POLAND
  89. PT: /^[A-Z]\d{6}$/,
  90. // PORTUGAL
  91. RO: /^\d{8,9}$/,
  92. // ROMANIA
  93. RU: /^\d{2}\d{2}\d{6}$/,
  94. // RUSSIAN FEDERATION
  95. SE: /^\d{8}$/,
  96. // SWEDEN
  97. SL: /^(P)[A-Z]\d{7}$/,
  98. // SLOVANIA
  99. SK: /^[0-9A-Z]\d{7}$/,
  100. // SLOVAKIA
  101. TR: /^[A-Z]\d{8}$/,
  102. // TURKEY
  103. UA: /^[A-Z]{2}\d{6}$/,
  104. // UKRAINE
  105. US: /^\d{9}$/ // UNITED STATES
  106. };
  107. /**
  108. * Check if str is a valid passport number
  109. * relative to provided ISO Country Code.
  110. *
  111. * @param {string} str
  112. * @param {string} countryCode
  113. * @return {boolean}
  114. */
  115. export default function isPassportNumber(str, countryCode) {
  116. assertString(str);
  117. /** Remove All Whitespaces, Convert to UPPERCASE */
  118. var normalizedStr = str.replace(/\s/g, '').toUpperCase();
  119. return countryCode.toUpperCase() in passportRegexByCountryCode && passportRegexByCountryCode[countryCode].test(normalizedStr);
  120. }