Skip to content

Instantly share code, notes, and snippets.

@jotaelesalinas
Created March 13, 2023 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jotaelesalinas/29bf002a13f2d7ee3f6194156306c94a to your computer and use it in GitHub Desktop.
Save jotaelesalinas/29bf002a13f2d7ee3f6194156306c94a to your computer and use it in GitHub Desktop.
IBAN validation Javascript class
class IBAN {
static code_lengths = {
AD: 24, AE: 23, AT: 20, AZ: 28, BA: 20, BE: 16, BG: 22, BH: 22, BR: 29, CH: 21,
CR: 21, CY: 28, CZ: 24, DE: 22, DK: 18, DO: 28, EE: 20, ES: 24, FI: 18, FO: 18,
FR: 27, GB: 22, GI: 23, GL: 18, GR: 27, GT: 28, HR: 21, HU: 28, IE: 22, IL: 23,
IS: 26, IT: 27, JO: 30, KW: 30, KZ: 20, LB: 28, LI: 21, LT: 20, LU: 20, LV: 21,
MC: 27, MD: 24, ME: 22, MK: 19, MR: 27, MT: 31, MU: 30, NL: 18, NO: 15, PK: 24,
PL: 28, PS: 29, PT: 25, QA: 29, RO: 24, RS: 22, SA: 24, SE: 24, SI: 19, SK: 24,
SM: 27, TN: 24, TR: 26, AL: 28, BY: 28, CR: 22, EG: 29, GE: 22, IQ: 23, LC: 32,
SC: 31, ST: 25, SV: 28, TL: 23, UA: 29, VA: 22, VG: 24, XK: 20,
};
static mod97 = (str) => {
var checksum = str.slice(0, 2), fragment;
for (var offset = 2; offset < str.length; offset += 7) {
fragment = String(checksum) + str.substring(offset, offset + 7);
checksum = parseInt(fragment, 10) % 97;
}
return checksum;
};
static isValid = (input) => {
// keep only alphanumeric characters
let iban = String(input).toUpperCase().replace(/[^A-Z0-9]/g, ''),
// match and capture (1) the country code, (2) the check digits, and (3) the rest
code = iban.match(/^([A-Z]{2})(\d{2})([A-Z\d]+)$/),
digits;
// check syntax and length
if (!code || iban.length !== IBAN.code_lengths[code[1]]) {
return false;
}
// rearrange country code and check digits, and convert chars to ints
digits = (code[3] + code[1] + code[2]).replace(/[A-Z]/g, function (letter) {
return letter.charCodeAt(0) - 55;
});
// final check
return IBAN.mod97(digits) === 1;
};
}
// Test suite for IBAN class
const testSuite = () => {
const validIbans = [
"GB82WEST12345698765432",
"FR7630006000011234567890189",
"DE75512108001245126199",
"ES9121000418450200051332",
"AT611904300234573201",
"BE68539007547034",
"CH9300762011623852957",
];
const invalidIbans = [
"GB81WEST12345698765432", // invalid check digit
"FR7630006000011234567890188", // invalid check digit
"DE75512108001245126198", // invalid check digit
"ES9121000418450200051331", // invalid check digit
"AT611904300234573200", // invalid length
"BE68539007547035", // invalid check digit
"CH9300762011623852958", // invalid check digit
"US64SVBKUS6S330095238", // invalid country code
"Not an IBAN", // invalid format
"", // empty string
];
console.log("Running tests...");
// Test isValid() method with valid IBANs
validIbans.forEach(iban => {
if (!IBAN.isValid(iban)) {
console.error(`Validation failed for valid IBAN: ${iban}`);
}
});
// Test isValid() method with invalid IBANs
invalidIbans.forEach(iban => {
if (IBAN.isValid(iban)) {
console.error(`Validation passed for invalid IBAN: ${iban}`);
}
});
console.log("Tests complete.");
};
// Run the test suite
testSuite();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment