Skip to content

Instantly share code, notes, and snippets.

@martijnluinstra
Last active May 17, 2022 01:39
Show Gist options
  • Save martijnluinstra/9ca0f62b9ddc01288be8a6072d29b029 to your computer and use it in GitHub Desktop.
Save martijnluinstra/9ca0f62b9ddc01288be8a6072d29b029 to your computer and use it in GitHub Desktop.
Dutch IBAN generator
/**
* Javascript Generator for Dutch IBANs.
* Demo: projects.martijnluinstra.nl/iban/
* Dependencies: https://github.com/MikeMcl/bignumber.js/
*
* Copyright (c) 2017 - Martijn Luinstra
*/
/**
* Converts IBAN to the decimal representation used for validation
*/
function ibanToNumber(iban) {
var code = iban.slice(4) + iban.slice(0,4);
code = code.toUpperCase();
var number = '';
for (var idx = 0; idx < code.length; idx++)
if (isNaN(code.charAt(idx)))
number += code.charCodeAt(idx) - 55;
else
number += code.charAt(idx);
return new BigNumber(number);
}
/**
* Validates IBAN based on checksum
*/
function validateIban(iban) {
return ibanToNumber(iban).mod(97).equals(1);
}
/**
* Generates elfproef-valid account number with based on numberic string
*/
function generateAccountNumber(unchecked) {
var total = 0
for (var idx=0; idx < unchecked.length; idx ++)
total += parseInt(unchecked.charAt(idx)) * (unchecked.length - idx + 1);
var check = 11 - (total % 11)
if (check > 0 && check < 10)
return unchecked + check.toString();
// Did not find a number that passes the elfproef, increase unchecked and try again
return generateAccountNumber((parseInt(unchecked) + 1).toString());
}
/**
* Generates realistic Dutch IBAN based on seed and (list of) bank code(s)
*/
function generateIban(seed, bankCode=null, country='NL', accountLength=9) {
var codes = seed.toUpperCase().split('').map(x => x.charCodeAt(0));
var number = codes.reduce((prod, value, idx) => prod.times(value + idx) , new BigNumber(1));
number = number.toFixed().replace(/0+$/,'');
var account = generateAccountNumber(number.slice(0, accountLength - 1));
account = account.padStart(accountLength + 1, '0');
if (!bankCode)
bankCode = 'BANK';
else if (bankCode instanceof Array) {
let idx = (number.charCodeAt(0) * number.charCodeAt(number.length - 1)) % bankCode.length;
bankCode = bankCode[idx];
}
var remainder = ibanToNumber(country + '00' + bankCode + account).mod(97);
check = (98 - remainder).toString().padStart(2, '0');
return country + check + bankCode + account;
}
<!--
Javascript Generator for Dutch IBANs.
Demo: projects.martijnluinstra.nl/iban/
Dependencies: https://github.com/MikeMcl/bignumber.js/
Copyright (c) 2017 Martijn Luinstra
-->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="mobileoptimized" content="0">
<title>IBAN</title>
<style>
body {
font-family: sans-serif;
}
h1 {
color: #8080ff;
}
label {
display: inline-block;
width: 4em;
font-weight: bold;
}
a,
a:hover,
a:visited,
a:focus {
color: #8080ff;
text-decoration: none;
}
a:hover,
a:focus {
text-decoration: underline;
}
.page {
max-width: 35em;
margin: 0 auto;
}
p.credits {
color: #999;
font-size: 65%;
}
</style>
</head>
<body>
<div class="page">
<h1>IBAN Generator</h1>
<p>
This tool generates a consistent and realistic Dutch IBAN for a seed. The resulting IBAN passes all IBAN validators that don't check whether the bank actually exists. The account number part (BBAN) of the generated IBANs satisfies the &ldquo;elfproef&rdquo; checksum for Dutch bank account numbers.
</p>
<p>
<label for="seed">Seed</label>
<input type="text" id="seed" name="seed" placeholder="Enter seed…">
</p>
<p>
<label for="iban">IBAN</label>
<input type="text" id="iban" name="iban" placeholder="…to generate IBAN" readonly>
</p>
<p class="credits">Created by <a href="https://www.martijnluinstra.nl/">Martijn Luinstra</a></p>
</div>
<script src="bignumber.js"></script>
<script src="iban.js"></script>
<script>
var el = document.getElementById('seed');
var target = document.getElementById('iban');
el.addEventListener('keyup', evt => {
if (el.value !== '')
target.value = generateIban(el.value);
else
target.value = '';
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment