Created
June 28, 2014 04:06
-
-
Save jahan-paisley/bff0bd0db8f387851cd2 to your computer and use it in GitHub Desktop.
IBAN Validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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