isIBAN.js 6.3 KB

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