0
顯然我的腳本是正確的,有人可以幫助我嗎?當我看到控制檯時,它顯示消息Cannot call method 'toString' of undefined
。無法調用undefined |的方法'toString'我的代碼有問題嗎?
這可能是ID選擇器的問題嗎?我可以只使用一個函數而不是使用一個函數進行驗證,而使用另一個函數運行我的函數?
/* ==== 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');
}
}
哪裏'cpf'得到界定? –
'undefined.toString()'< - 容易重現;是的,某些*是錯誤的。因此,找出爲什麼存在未定義與「預期」值。 1)調用函數時,cpf綁定爲undefined,或者; 2)分割只返回一個結果,如果分隔符丟失,這是可能的。 – user2246674
在控制檯的哪一行指示錯誤正在發生? –