First of all, thank you guys for always helping me out around here, I could not be working on my actual job without the orientation of these forums. I’m quite new to javascript so please forgive any mistakes I might have done (or do in the future).
I am building a Webapp for a costumer in which I need to check if the user’s social security number (here we call it CPF) is valid. CPF has an algorithm for checking the last two digits, I got this code from our IRS web site so it’s pretty neat. (Also, there’s a valid CPF generator I’m using for testing on their webste: http://www.geradorcpf.com/)
var cpf=Input Insert the CPF here’s value;
function validarCPF() {
cpf = cpf.replace(/[^\d]+/g,’’);
if(cpf == ‘’) return false;
// Elimina CPFs invalidos conhecidos
if (cpf.length != 11 || cpf == “00000000000” || cpf == “11111111111” || cpf == “22222222222” || cpf == “33333333333” || cpf == “44444444444” || cpf == “55555555555” || cpf == “66666666666” || cpf == “77777777777” || cpf == “88888888888” || cpf == “99999999999”)
return false;
// Valida 1o digito
add = 0;
for (i=0; i < 9; i ++)
add += parseInt(cpf.charAt(i)) * (10 - i);
rev = 11 - (add % 11);
if (rev == 10 || rev == 11)
rev = 0;
if (rev != parseInt(cpf.charAt(9)))
return false;
// Valida 2o digito
add = 0;
for (i = 0; i < 10; i ++)
add += parseInt(cpf.charAt(i)) * (11 - i);
rev = 11 - (add % 11);
if (rev == 10 || rev == 11)
rev = 0;
if (rev != parseInt(cpf.charAt(10)))
return false;
return true;
}
bubble_fn_valid(validarCPF());
So, somehow it is always returning false…
Sorry if this was already asked, I searched for a while (including the showcase for Toolbox) and couldn’t find any answers (they helped A LOT, but no answer specifically for this problem.)
Thank you ahead for the time and patience.
ALSO, this is the link of an example app for you to help me figuring this one out.
TESTCPF Workflow
EDIT: WHOA! I found out one thing I was doing wrong rereading the Toolbox’s Showcase right now. I was thinking you could use the JavascriptToBubble as a variable, but you can’t. But you can use it like this:
So I created a variable and assigned it to bubble_fn_valid now, it still doesn’t work, it always returns false. Ah, I updated the whole text.