Skip to content

Instantly share code, notes, and snippets.

@jahan-paisley
Created June 28, 2014 04:06
Show Gist options
  • Save jahan-paisley/bff0bd0db8f387851cd2 to your computer and use it in GitHub Desktop.
Save jahan-paisley/bff0bd0db8f387851cd2 to your computer and use it in GitHub Desktop.
IBAN Validation
// Modulo 97 for huge numbers given as digit strings.
function mod97(digit_string)
{
var m = 0;
for (var i = 0; i < digit_string.length; ++i)
m = (m * 10 + parseInt(digit_string.charAt(i))) % 97;
return m;
}
// Calculate 2-digit checksum of an IBAN.
/**
* @return {number}
*/
function ChecksumIBAN(iban)
{
var code = iban.substring(0, 2);
var checksum = iban.substring(2, 4);
var bban = iban.substring(4);
// Assemble digit string
var digits = "";
for (var i = 0; i < bban.length; ++i)
{
var ch = bban.charAt(i).toUpperCase();
if ("0" <= ch && ch <= "9")
digits += ch;
else
digits += capital2digits(ch);
}
for (var i = 0; i < code.length; ++i)
{
var ch = code.charAt(i);
digits += capital2digits(ch);
}
digits += checksum;
// Calculate checksum
return 98 - mod97(digits);
}
// Convert a capital letter into digits: A -> 10 ... Z -> 35 (ISO 13616).
function capital2digits(ch)
{
var capitals = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (var i = 0; i < capitals.length; ++i)
if (ch == capitals.charAt(i))
break;
return i + 10;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment