Created
May 8, 2013 14:44
-
-
Save krypton/5540925 to your computer and use it in GitHub Desktop.
Javascript IBAN (International Bank Account Number) Validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
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
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