Skip to content

Instantly share code, notes, and snippets.

@rmontagud
Created October 1, 2010 10:28
Show Gist options
  • Save rmontagud/606031 to your computer and use it in GitHub Desktop.
Save rmontagud/606031 to your computer and use it in GitHub Desktop.
/**
* Spanish Bank Account Number validation
*/
function saneaFormato(str, num) {
return '' + new Array((num - str.toString().length)+1).join('0') + str;
}
function mod11(x) {
var pesos = [ 1, 2, 4, 8, 5, 10, 9, 7, 3, 6 ], control = 0, i, offset = pesos.length - x.length;
for (i = 0; i < x.length; i++) control += x.charAt(i) * pesos[i+offset];
control = 11 - (control % 11);
if (control == 11) control=0;
if (control == 10) control=1;
return control;
}
function validaCC( banco, oficina, dcontrol, ncuenta){
var entoficina = 0, ccorriente = 0, i;
entoficina = mod11(saneaFormato(banco, 4) + '' + saneaFormato(oficina, 4));
ccorriente = mod11(saneaFormato(ncuenta, 10));
if (entoficina*10+ccorriente != saneaFormato(dcontrol, 2)) return false;
return true;
}
@rmontagud
Copy link
Author

It lacks some checks but as long as you keep the numbers in the range it will work

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