Skip to content

Instantly share code, notes, and snippets.

@atk
Forked from 140bytes/LICENSE.txt
Last active December 24, 2015 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atk/6776857 to your computer and use it in GitHub Desktop.
Save atk/6776857 to your computer and use it in GitHub Desktop.
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[0], 36)+i(n[1], 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[4] % 97 + d[5]) % 97 + "" + i(d[1], 36) + i(d[2], 36) + d[3]) % 97 == 1
}
function(b,a,n,i){i=parseInt;return n+('0'+(98-((((""+b)%97+(""+a))%97)+""+i(n[0],36)+i(n[1],36)+"00")%97)).slice(-2)+b+a}
function(i,d){return(d=/(\w)(\w)(..)(\d{8})(\d+)/.exec(i))&&(i=parseInt,(d[4]%97+d[5])%97+""+i(d[1],36)+i(d[2],36)+d[3])%97==1}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2013 Alex Kloss <alexthkloss@web.de>
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"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b>DE76700901000123456789, true</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var iBAN = function(b,a,n,i){i=parseInt;return n+('0'+(98-((((""+b)%97+(""+a))%97)+""+i(n[0],36)+i(n[1],36)+"00")%97)).slice(-2)+b+a},
isiBAN = function(i,d){return(d=/(\w)(\w)(..)(\d{8})(\d+)/.exec(i))&&(i=parseInt,(d[4]%97+d[5])%97+""+i(d[1],36)+i(d[2],36)+d[3])%97==1}
document.getElementById( "ret" ).innerHTML = [iBAN('70090100','0123456789','DE'), isiBAN("DE76700901000123456789")].join(', ')
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment