I wrote a JavaScript function for validating bank account numbers in IBAN format for my own joy. Could you have a look if you don't see any obvious mistakes or places for improvement?
function isIbanValid(value) {
var lengthMap = getLengthMap();
//cleanup
value = value.toString().toUpperCase().replace(/\s/g, '').replace(/[-]/g, '');
//check if alphanumeric
if (!/^[a-zA-Z0-9]+$/.test(value)) return false;
//extract countrycode
var countryCode = value.substring(0, 2);
//check if letter
if (!/([a-z]+[\s\-]?)*/i.test(countryCode)) return false;
//check string length
if (value.length != lengthMap[countryCode]) return false;
value = value.concat(value.substring(0, 4)).substring(4);
value = value.replace(countryCode, countryCodeToStringValue(countryCode));
return modulo(value, 97) == 1;
function countryCodeToStringValue(countryCode) {
return "".concat(((countryCode.charCodeAt(0)) - 55).toString() + (countryCode.charCodeAt(1) - 55).toString());
}
function modulo(divident, divisor) {
var quantization = 12;
while (quantization < divident.length) {
var part = divident.substring(0, quantization);
divident = (part % divisor) + divident.substring(quantization);
}
return divident % divisor;
}
function getLengthMap() {
var lengthMap = {};
lengthMap["AD"] = 24;
lengthMap["AT"] = 20;
lengthMap["AZ"] = 28;
lengthMap["BH"] = 22;
lengthMap["BE"] = 16;
lengthMap["BA"] = 20;
lengthMap["BR"] = 29;
lengthMap["BG"] = 22;
lengthMap["CR"] = 21;
lengthMap["HR"] = 21;
lengthMap["CY"] = 28;
lengthMap["CZ"] = 24;
lengthMap["DK"] = 18;
lengthMap["DO"] = 28;
lengthMap["EE"] = 20;
lengthMap["FO"] = 18;
lengthMap["FI"] = 18;
lengthMap["FR"] = 27;
lengthMap["DE"] = 22;
lengthMap["GR"] = 27;
lengthMap["GI"] = 23;
lengthMap["GL"] = 18;
lengthMap["GT"] = 28;
lengthMap["HU"] = 28;
lengthMap["IS"] = 26;
lengthMap["IE"] = 22;
lengthMap["IL"] = 23;
lengthMap["IT"] = 27;
lengthMap["JO"] = 30;
lengthMap["KZ"] = 20;
lengthMap["KW"] = 30;
lengthMap["LV"] = 21;
lengthMap["LB"] = 28;
lengthMap["LI"] = 21;
lengthMap["LT"] = 20;
lengthMap["LU"] = 20;
lengthMap["MK"] = 19;
lengthMap["MT"] = 31;
lengthMap["MR"] = 27;
lengthMap["MU"] = 30;
lengthMap["MC"] = 27;
lengthMap["MD"] = 24;
lengthMap["ME"] = 22;
lengthMap["NL"] = 18;
lengthMap["NO"] = 15;
lengthMap["PK"] = 24;
lengthMap["PS"] = 29;
lengthMap["PL"] = 28;
lengthMap["PT"] = 25;
lengthMap["QA"] = 29;
lengthMap["RO"] = 24;
lengthMap["SM"] = 27;
lengthMap["SA"] = 24;
lengthMap["RS"] = 22;
lengthMap["SK"] = 24;
lengthMap["SI"] = 19;
lengthMap["ES"] = 24;
lengthMap["SE"] = 24;
lengthMap["CH"] = 21;
lengthMap["TN"] = 24;
lengthMap["TR"] = 26;
lengthMap["AE"] = 23;
lengthMap["GB"] = 22;
return lengthMap;
}
}
dividend
, notdivident
. Your meaning still comes across clearly, but it doesn't hurt to fix it. \$\endgroup\$