Instantly share code, notes, and snippets.

# atk/LICENSE.txt forked from 140bytes/LICENSE.txt Last active Dec 24, 2015

iBAN helper functions

Two little helpers, one to calculate an iBAN (international banking account number) for any bank account and one to verify its consistency.

Since iBAN numbers are generally too long to be calculated within JS, the modulo calculation is splitted.

If you need to use the iBAN calculator in older versions, of MSIE that lack the ability to address a string's characters like they were in an array, use the following (longer) version:

``````function(b,a,n,i){i=parseInt;return n+('0'+(98-((((""+b)%97+(""+a))%97)+""+i(n.charAt(0),36)+i(n.charAt(1),36)+"00")%97)).slice(-2)+b+a}
``````
 // calculate iBAN from bank id, account number and nation code function( b, // bank id a, // account number (padded to 10 chars) n, // nation code (e.g. "DE") i // placeholder for parseInt ){ i = parseInt; return n + // concatenate nation code, checksum, bank id and account number // calculate checksum (bank id, account number, country code base36 + '00') modulo 97 // padded with 0 if necessary ('0' + (98 - (((("" + b) % 97 + ("" + a)) % 97) + "" + i(n, 36)+i(n, 36) + "00") % 97)).slice(-2) + b + a } // verify iBAN checksum function( i, // iBAN string d // placeholder for matches ){ // match country code, checksum, bank id and account number return (d = /(\w)(\w)(..)(\d{8})(\d+)/.exec(i)) && (i = parseInt, // store parseInt for smaller function // bank id + account + country code base 36 decoded + checksum % 97 must be 1 // for every correct iBAN (d % 97 + d) % 97 + "" + i(d, 36) + i(d, 36) + d) % 97 == 1 }
 function(b,a,n,i){i=parseInt;return n+('0'+(98-((((""+b)%97+(""+a))%97)+""+i(n,36)+i(n,36)+"00")%97)).slice(-2)+b+a} function(i,d){return(d=/(\w)(\w)(..)(\d{8})(\d+)/.exec(i))&&(i=parseInt,(d%97+d)%97+""+i(d,36)+i(d,36)+d)%97==1}
 DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004 Copyright (C) 2013 Alex Kloss Everyone is permitted to copy and distribute verbatim or modified copies of this license document, and changing it is allowed as long as the name is changed. DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. You just DO WHAT THE FUCK YOU WANT TO.
 { "name": "iBAN tools", "description": "iBAN calculator / verification", "keywords": [ "iBAN", "banking", "calculation", "checksum", "verification" ] }
 Foo
Expected value: DE76700901000123456789, true
Actual value: