isIBAN.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. MA: /^(MA[0-9]{26})$/,
  63. MC: /^(MC[0-9]{2})\d{10}[A-Z0-9]{11}\d{2}$/,
  64. MD: /^(MD[0-9]{2})[A-Z0-9]{20}$/,
  65. ME: /^(ME[0-9]{2})\d{18}$/,
  66. MK: /^(MK[0-9]{2})\d{3}[A-Z0-9]{10}\d{2}$/,
  67. MR: /^(MR[0-9]{2})\d{23}$/,
  68. MT: /^(MT[0-9]{2})[A-Z]{4}\d{5}[A-Z0-9]{18}$/,
  69. MU: /^(MU[0-9]{2})[A-Z]{4}\d{19}[A-Z]{3}$/,
  70. MZ: /^(MZ[0-9]{2})\d{21}$/,
  71. NL: /^(NL[0-9]{2})[A-Z]{4}\d{10}$/,
  72. NO: /^(NO[0-9]{2})\d{11}$/,
  73. PK: /^(PK[0-9]{2})[A-Z0-9]{4}\d{16}$/,
  74. PL: /^(PL[0-9]{2})\d{24}$/,
  75. PS: /^(PS[0-9]{2})[A-Z0-9]{4}\d{21}$/,
  76. PT: /^(PT[0-9]{2})\d{21}$/,
  77. QA: /^(QA[0-9]{2})[A-Z]{4}[A-Z0-9]{21}$/,
  78. RO: /^(RO[0-9]{2})[A-Z]{4}[A-Z0-9]{16}$/,
  79. RS: /^(RS[0-9]{2})\d{18}$/,
  80. SA: /^(SA[0-9]{2})\d{2}[A-Z0-9]{18}$/,
  81. SC: /^(SC[0-9]{2})[A-Z]{4}\d{20}[A-Z]{3}$/,
  82. SE: /^(SE[0-9]{2})\d{20}$/,
  83. SI: /^(SI[0-9]{2})\d{15}$/,
  84. SK: /^(SK[0-9]{2})\d{20}$/,
  85. SM: /^(SM[0-9]{2})[A-Z]{1}\d{10}[A-Z0-9]{12}$/,
  86. SV: /^(SV[0-9]{2})[A-Z0-9]{4}\d{20}$/,
  87. TL: /^(TL[0-9]{2})\d{19}$/,
  88. TN: /^(TN[0-9]{2})\d{20}$/,
  89. TR: /^(TR[0-9]{2})\d{5}[A-Z0-9]{17}$/,
  90. UA: /^(UA[0-9]{2})\d{6}[A-Z0-9]{19}$/,
  91. VA: /^(VA[0-9]{2})\d{18}$/,
  92. VG: /^(VG[0-9]{2})[A-Z0-9]{4}\d{16}$/,
  93. XK: /^(XK[0-9]{2})\d{16}$/
  94. };
  95. /**
  96. * Check if the country codes passed are valid using the
  97. * ibanRegexThroughCountryCode as a reference
  98. *
  99. * @param {array} countryCodeArray
  100. * @return {boolean}
  101. */
  102. function hasOnlyValidCountryCodes(countryCodeArray) {
  103. var countryCodeArrayFilteredWithObjectIbanCode = countryCodeArray.filter(function (countryCode) {
  104. return !(countryCode in ibanRegexThroughCountryCode);
  105. });
  106. if (countryCodeArrayFilteredWithObjectIbanCode.length > 0) {
  107. return false;
  108. }
  109. return true;
  110. }
  111. /**
  112. * Check whether string has correct universal IBAN format
  113. * The IBAN consists of up to 34 alphanumeric characters, as follows:
  114. * Country Code using ISO 3166-1 alpha-2, two letters
  115. * check digits, two digits and
  116. * Basic Bank Account Number (BBAN), up to 30 alphanumeric characters.
  117. * NOTE: Permitted IBAN characters are: digits [0-9] and the 26 latin alphabetic [A-Z]
  118. *
  119. * @param {string} str - string under validation
  120. * @param {object} options - object to pass the countries to be either whitelisted or blacklisted
  121. * @return {boolean}
  122. */
  123. function hasValidIbanFormat(str, options) {
  124. // Strip white spaces and hyphens
  125. var strippedStr = str.replace(/[\s\-]+/gi, '').toUpperCase();
  126. var isoCountryCode = strippedStr.slice(0, 2).toUpperCase();
  127. var isoCountryCodeInIbanRegexCodeObject = (isoCountryCode in ibanRegexThroughCountryCode);
  128. if (options.whitelist) {
  129. if (!hasOnlyValidCountryCodes(options.whitelist)) {
  130. return false;
  131. }
  132. var isoCountryCodeInWhiteList = options.whitelist.includes(isoCountryCode);
  133. if (!isoCountryCodeInWhiteList) {
  134. return false;
  135. }
  136. }
  137. if (options.blacklist) {
  138. var isoCountryCodeInBlackList = options.blacklist.includes(isoCountryCode);
  139. if (isoCountryCodeInBlackList) {
  140. return false;
  141. }
  142. }
  143. return isoCountryCodeInIbanRegexCodeObject && ibanRegexThroughCountryCode[isoCountryCode].test(strippedStr);
  144. }
  145. /**
  146. * Check whether string has valid IBAN Checksum
  147. * by performing basic mod-97 operation and
  148. * the remainder should equal 1
  149. * -- Start by rearranging the IBAN by moving the four initial characters to the end of the string
  150. * -- Replace each letter in the string with two digits, A -> 10, B = 11, Z = 35
  151. * -- Interpret the string as a decimal integer and
  152. * -- compute the remainder on division by 97 (mod 97)
  153. * Reference: https://en.wikipedia.org/wiki/International_Bank_Account_Number
  154. *
  155. * @param {string} str
  156. * @return {boolean}
  157. */
  158. function hasValidIbanChecksum(str) {
  159. var strippedStr = str.replace(/[^A-Z0-9]+/gi, '').toUpperCase(); // Keep only digits and A-Z latin alphabetic
  160. var rearranged = strippedStr.slice(4) + strippedStr.slice(0, 4);
  161. var alphaCapsReplacedWithDigits = rearranged.replace(/[A-Z]/g, function (char) {
  162. return char.charCodeAt(0) - 55;
  163. });
  164. var remainder = alphaCapsReplacedWithDigits.match(/\d{1,7}/g).reduce(function (acc, value) {
  165. return Number(acc + value) % 97;
  166. }, '');
  167. return remainder === 1;
  168. }
  169. function isIBAN(str) {
  170. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  171. (0, _assertString.default)(str);
  172. return hasValidIbanFormat(str, options) && hasValidIbanChecksum(str);
  173. }
  174. var locales = Object.keys(ibanRegexThroughCountryCode);
  175. exports.locales = locales;