isIBAN.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = isIBAN;
  6. exports.locales = void 0;
  7. var _assertString = _interopRequireDefault(require("./util/assertString"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. /**
  10. * List of country codes with
  11. * corresponding IBAN regular expression
  12. * Reference: https://en.wikipedia.org/wiki/International_Bank_Account_Number
  13. */
  14. var ibanRegexThroughCountryCode = {
  15. AD: /^(AD[0-9]{2})\d{8}[A-Z0-9]{12}$/,
  16. AE: /^(AE[0-9]{2})\d{3}\d{16}$/,
  17. AL: /^(AL[0-9]{2})\d{8}[A-Z0-9]{16}$/,
  18. AT: /^(AT[0-9]{2})\d{16}$/,
  19. AZ: /^(AZ[0-9]{2})[A-Z0-9]{4}\d{20}$/,
  20. BA: /^(BA[0-9]{2})\d{16}$/,
  21. BE: /^(BE[0-9]{2})\d{12}$/,
  22. BG: /^(BG[0-9]{2})[A-Z]{4}\d{6}[A-Z0-9]{8}$/,
  23. BH: /^(BH[0-9]{2})[A-Z]{4}[A-Z0-9]{14}$/,
  24. BR: /^(BR[0-9]{2})\d{23}[A-Z]{1}[A-Z0-9]{1}$/,
  25. BY: /^(BY[0-9]{2})[A-Z0-9]{4}\d{20}$/,
  26. CH: /^(CH[0-9]{2})\d{5}[A-Z0-9]{12}$/,
  27. CR: /^(CR[0-9]{2})\d{18}$/,
  28. CY: /^(CY[0-9]{2})\d{8}[A-Z0-9]{16}$/,
  29. CZ: /^(CZ[0-9]{2})\d{20}$/,
  30. DE: /^(DE[0-9]{2})\d{18}$/,
  31. DK: /^(DK[0-9]{2})\d{14}$/,
  32. DO: /^(DO[0-9]{2})[A-Z]{4}\d{20}$/,
  33. EE: /^(EE[0-9]{2})\d{16}$/,
  34. EG: /^(EG[0-9]{2})\d{25}$/,
  35. ES: /^(ES[0-9]{2})\d{20}$/,
  36. FI: /^(FI[0-9]{2})\d{14}$/,
  37. FO: /^(FO[0-9]{2})\d{14}$/,
  38. FR: /^(FR[0-9]{2})\d{10}[A-Z0-9]{11}\d{2}$/,
  39. GB: /^(GB[0-9]{2})[A-Z]{4}\d{14}$/,
  40. GE: /^(GE[0-9]{2})[A-Z0-9]{2}\d{16}$/,
  41. GI: /^(GI[0-9]{2})[A-Z]{4}[A-Z0-9]{15}$/,
  42. GL: /^(GL[0-9]{2})\d{14}$/,
  43. GR: /^(GR[0-9]{2})\d{7}[A-Z0-9]{16}$/,
  44. GT: /^(GT[0-9]{2})[A-Z0-9]{4}[A-Z0-9]{20}$/,
  45. HR: /^(HR[0-9]{2})\d{17}$/,
  46. HU: /^(HU[0-9]{2})\d{24}$/,
  47. IE: /^(IE[0-9]{2})[A-Z0-9]{4}\d{14}$/,
  48. IL: /^(IL[0-9]{2})\d{19}$/,
  49. IQ: /^(IQ[0-9]{2})[A-Z]{4}\d{15}$/,
  50. IR: /^(IR[0-9]{2})0\d{2}0\d{18}$/,
  51. IS: /^(IS[0-9]{2})\d{22}$/,
  52. IT: /^(IT[0-9]{2})[A-Z]{1}\d{10}[A-Z0-9]{12}$/,
  53. JO: /^(JO[0-9]{2})[A-Z]{4}\d{22}$/,
  54. KW: /^(KW[0-9]{2})[A-Z]{4}[A-Z0-9]{22}$/,
  55. KZ: /^(KZ[0-9]{2})\d{3}[A-Z0-9]{13}$/,
  56. LB: /^(LB[0-9]{2})\d{4}[A-Z0-9]{20}$/,
  57. LC: /^(LC[0-9]{2})[A-Z]{4}[A-Z0-9]{24}$/,
  58. LI: /^(LI[0-9]{2})\d{5}[A-Z0-9]{12}$/,
  59. LT: /^(LT[0-9]{2})\d{16}$/,
  60. LU: /^(LU[0-9]{2})\d{3}[A-Z0-9]{13}$/,
  61. LV: /^(LV[0-9]{2})[A-Z]{4}[A-Z0-9]{13}$/,
  62. MC: /^(MC[0-9]{2})\d{10}[A-Z0-9]{11}\d{2}$/,
  63. MD: /^(MD[0-9]{2})[A-Z0-9]{20}$/,
  64. ME: /^(ME[0-9]{2})\d{18}$/,
  65. MK: /^(MK[0-9]{2})\d{3}[A-Z0-9]{10}\d{2}$/,
  66. MR: /^(MR[0-9]{2})\d{23}$/,
  67. MT: /^(MT[0-9]{2})[A-Z]{4}\d{5}[A-Z0-9]{18}$/,
  68. MU: /^(MU[0-9]{2})[A-Z]{4}\d{19}[A-Z]{3}$/,
  69. MZ: /^(MZ[0-9]{2})\d{21}$/,
  70. NL: /^(NL[0-9]{2})[A-Z]{4}\d{10}$/,
  71. NO: /^(NO[0-9]{2})\d{11}$/,
  72. PK: /^(PK[0-9]{2})[A-Z0-9]{4}\d{16}$/,
  73. PL: /^(PL[0-9]{2})\d{24}$/,
  74. PS: /^(PS[0-9]{2})[A-Z0-9]{4}\d{21}$/,
  75. PT: /^(PT[0-9]{2})\d{21}$/,
  76. QA: /^(QA[0-9]{2})[A-Z]{4}[A-Z0-9]{21}$/,
  77. RO: /^(RO[0-9]{2})[A-Z]{4}[A-Z0-9]{16}$/,
  78. RS: /^(RS[0-9]{2})\d{18}$/,
  79. SA: /^(SA[0-9]{2})\d{2}[A-Z0-9]{18}$/,
  80. SC: /^(SC[0-9]{2})[A-Z]{4}\d{20}[A-Z]{3}$/,
  81. SE: /^(SE[0-9]{2})\d{20}$/,
  82. SI: /^(SI[0-9]{2})\d{15}$/,
  83. SK: /^(SK[0-9]{2})\d{20}$/,
  84. SM: /^(SM[0-9]{2})[A-Z]{1}\d{10}[A-Z0-9]{12}$/,
  85. SV: /^(SV[0-9]{2})[A-Z0-9]{4}\d{20}$/,
  86. TL: /^(TL[0-9]{2})\d{19}$/,
  87. TN: /^(TN[0-9]{2})\d{20}$/,
  88. TR: /^(TR[0-9]{2})\d{5}[A-Z0-9]{17}$/,
  89. UA: /^(UA[0-9]{2})\d{6}[A-Z0-9]{19}$/,
  90. VA: /^(VA[0-9]{2})\d{18}$/,
  91. VG: /^(VG[0-9]{2})[A-Z0-9]{4}\d{16}$/,
  92. XK: /^(XK[0-9]{2})\d{16}$/
  93. };
  94. /**
  95. * Check whether string has correct universal IBAN format
  96. * The IBAN consists of up to 34 alphanumeric characters, as follows:
  97. * Country Code using ISO 3166-1 alpha-2, two letters
  98. * check digits, two digits and
  99. * Basic Bank Account Number (BBAN), up to 30 alphanumeric characters.
  100. * NOTE: Permitted IBAN characters are: digits [0-9] and the 26 latin alphabetic [A-Z]
  101. *
  102. * @param {string} str - string under validation
  103. * @return {boolean}
  104. */
  105. function hasValidIbanFormat(str) {
  106. // Strip white spaces and hyphens
  107. var strippedStr = str.replace(/[\s\-]+/gi, '').toUpperCase();
  108. var isoCountryCode = strippedStr.slice(0, 2).toUpperCase();
  109. return isoCountryCode in ibanRegexThroughCountryCode && ibanRegexThroughCountryCode[isoCountryCode].test(strippedStr);
  110. }
  111. /**
  112. * Check whether string has valid IBAN Checksum
  113. * by performing basic mod-97 operation and
  114. * the remainder should equal 1
  115. * -- Start by rearranging the IBAN by moving the four initial characters to the end of the string
  116. * -- Replace each letter in the string with two digits, A -> 10, B = 11, Z = 35
  117. * -- Interpret the string as a decimal integer and
  118. * -- compute the remainder on division by 97 (mod 97)
  119. * Reference: https://en.wikipedia.org/wiki/International_Bank_Account_Number
  120. *
  121. * @param {string} str
  122. * @return {boolean}
  123. */
  124. function hasValidIbanChecksum(str) {
  125. var strippedStr = str.replace(/[^A-Z0-9]+/gi, '').toUpperCase(); // Keep only digits and A-Z latin alphabetic
  126. var rearranged = strippedStr.slice(4) + strippedStr.slice(0, 4);
  127. var alphaCapsReplacedWithDigits = rearranged.replace(/[A-Z]/g, function (char) {
  128. return char.charCodeAt(0) - 55;
  129. });
  130. var remainder = alphaCapsReplacedWithDigits.match(/\d{1,7}/g).reduce(function (acc, value) {
  131. return Number(acc + value) % 97;
  132. }, '');
  133. return remainder === 1;
  134. }
  135. function isIBAN(str) {
  136. (0, _assertString.default)(str);
  137. return hasValidIbanFormat(str) && hasValidIbanChecksum(str);
  138. }
  139. var locales = Object.keys(ibanRegexThroughCountryCode);
  140. exports.locales = locales;