Created
October 1, 2010 10:28
-
-
Save rmontagud/606031 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It lacks some checks but as long as you keep the numbers in the range it will work