Skip to content

Instantly share code, notes, and snippets.

@krypton
Created May 8, 2013 14:44
Show Gist options
  • Save krypton/5540925 to your computer and use it in GitHub Desktop.
Save krypton/5540925 to your computer and use it in GitHub Desktop.
Javascript IBAN (International Bank Account Number) Validation
function validateIBAN(iban) {
var newIban = iban.toUpperCase(),
modulo = function (divident, divisor) {
var cDivident = '';
var cRest = '';
for (var i in divident ) {
var cChar = divident[i];
var cOperator = cRest + '' + cDivident + '' + cChar;
if ( cOperator < parseInt(divisor) ) {
cDivident += '' + cChar;
} else {
cRest = cOperator % divisor;
if ( cRest == 0 ) {
cRest = '';
}
cDivident = '';
}
}
cRest += '' + cDivident;
if (cRest == '') {
cRest = 0;
}
return cRest;
};
if (newIban.search(/^[A-Z]{2}/gi) < 0) {
return false;
}
newIban = newIban.substring(4) + newIban.substring(0, 4);
newIban = newIban.replace(/[A-Z]/g, function (match) {
return match.charCodeAt(0) - 55;
});
return parseInt(modulo(newIban, 97), 10) === 1;
}
@LaurentVB
Copy link

I'm late at the party, but I'm shamelessly plugging my own lib for validating and formatting IBAN & BBAN: https://github.com/arhs/iban.js

@sorliej
Copy link

sorliej commented Aug 8, 2014

Laurent, your code is very cool. I pasted it into an Apps Script "as is" and it works great with the following "test bench" code in the Code.gs file:

// Use the Apps Script IDE's "Run" menu to execute this code and then look at the View > Logs to see results.

function myFunction() {
//https://github.com/arhs/iban.js/blob/master/README.md
// var IBAN = require('iban');
var t1 = IBAN.isValid('hello world'); // false
var t2 = IBAN.isValid('BE68539007547034'); // true
var t3 = IBAN.isValid('BE68 5390 0754 7034'); // true

Logger.log("Test 1 = %s", t1);
Logger.log("Test 2 = %s", t2);
Logger.log("Test 3 = %s", t3);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment