Skip to content

Instantly share code, notes, and snippets.

@danielhusar
Forked from krypton/ibanvalidator.js
Last active December 22, 2015 15:39
Show Gist options
  • Save danielhusar/6493715 to your computer and use it in GitHub Desktop.
Save danielhusar/6493715 to your computer and use it in GitHub Desktop.
function validateIBAN(iban) {
var newIban = iban.replace(/ /g, '').toUpperCase(),
modulo = function (divident, divisor) {
var cDivident = '';
var cRest = '';
for (var i in divident ) {
var cChar = divident[i];
var cOperator = cRest + '' + cDivident + '' + cChar;
if ( cOperator < parseInt(divisor) ) {
cDivident += '' + cChar;
} else {
cRest = cOperator % divisor;
if ( cRest == 0 ) {
cRest = '';
}
cDivident = '';
}
}
cRest += '' + cDivident;
if (cRest == '') {
cRest = 0;
}
return cRest;
};
if (newIban.search(/^[A-Z]{2}/gi) < 0) {
return false;
}
newIban = newIban.substring(4) + newIban.substring(0, 4);
newIban = newIban.replace(/[A-Z]/g, function (match) {
return match.charCodeAt(0) - 55;
});
return parseInt(modulo(newIban, 97), 10) === 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment