isIBAN.js 6.3 KB

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