Apparently my script is right, could someone help me with this? When I see the console it shows the message Cannot call method 'toString' of undefined
.
Could it be a problem with the ID selector? Can I use just one function instead of using one for validation and another to run my function?
/* ==== CPF Validator ==== */
function validar_cpf(cpf)
{
regex = /\./g;
cpf = cpf.toString().replace(regex,'');
cpf_split = cpf.split('-');
numero = cpf_split[0];
dv = cpf_split[1];
numero_init = numero.toString() + dv.toString();
if(numero_init.length < 11)
return false
var somatorio = 0;
for(i = 10, n = 0; i >= 2 ; i--, n++){
m = numero.charAt(n) * i;
somatorio += m;
}
dv1 = somatorio / 11;
dv1 = parseInt(dv1);
resto = somatorio % 11;
if(resto < 2)
dv1 = 0;
else
dv1 = Math.abs(11 - resto);
numero += dv1.toString();
somatorio = 0;
for(i = 11, n= 0; i >= 2 ; i--, n++ ){
m = numero.charAt(n) * i;
somatorio += m;
}
resto = somatorio % 11;
if(resto < 2)
dv2 = 0;
else
dv2 = 11 - resto;
numero += dv2.toString();
if(numero == numero_init)
return true;
else
return false;
}
function ValidaCpf()
{
if (validar_cpf(cpf))
{
document.getElementById('first-cpf').classList.add('hide');
document.getElementById('second-cpf').classList.remove('hide');
document.getElementById('cpf').classList.add('form_invalido');
document.getElementById('cpf').classList.remove('form_valido');
}
else {
document.getElementById('first-cpf').classList.remove('hide');
document.getElementById('second-cpf').classList.add('hide');
document.getElementById('cpf').classList.remove('form_invalido');
document.getElementById('cpf').classList.add('form_valido');
}
}