-
-
Save krypton/5540925 to your computer and use it in GitHub Desktop.
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; | |
} |
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
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);
}
Failing on UK IBAN's sadly. Anyone got an idea about how to fix it?