Skip to content

Instantly share code, notes, and snippets.

@krypton
Created May 8, 2013 14:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • 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;
}
@mangrar
Copy link

mangrar commented Sep 13, 2013

Hi,

Your modulo function doesn't work, I have tried with some accounts and it always returns NaN. Try this:

var modulo = function(divident, divisor)
{
  var m = 0;
  for (var i = 0; i < divident.length; ++i)
      m = (m * 10 + parseInt(divident.charAt(i))) % divisor;
  return m;
};

Regards

@magalhini
Copy link

Failing on UK IBAN's sadly. Anyone got an idea about how to fix it?

@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