【问题标题】:Cannot call method 'toString' of undefined | There is something wrong with my code?无法调用未定义的方法“toString”|我的代码有问题吗?
【发布时间】:2013-07-16 19:10:48
【问题描述】:

显然我的脚本是正确的,有人可以帮我解决这个问题吗?当我看到控制台时,它会显示消息Cannot call method 'toString' of undefined

会不会是 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)拆分只返回一个结果,如果缺少分隔符,这是可能的。
  • 控制台在哪一行显示错误正在发生?
  • 就行 numero_init = numero.toString() + dv.toString();
  • 如果该行出错,很可能是“dv”未定义,这意味着分隔符不存在于cpf中。

标签: javascript html dom frontend


【解决方案1】:

我找到了解决办法

/* ==== CPF Validator ==== */
function ValidaCpf(input) {
    var value = input.value,
        result = true,
        invalids = new Array(
        '111.111.111-11',
        '222.222.222-22',
        '333.333.333-33',
        '444.444.444-44',
        '555.555.555-55',
        '666.666.666-66',
        '777.777.777-77',
        '888.888.888-88',
        '999.999.999-99',
        '000.000.000-00',

        '11111111111',
        '22222222222',
        '33333333333',
        '44444444444',
        '55555555555',
        '66666666666',
        '77777777777',
        '88888888888',
        '99999999999',
        '00000000000'
      );

    for( var i = 0; i<invalids.length; i++) {
      if( invalids[i] == value) {
        result = false;
      }
    }

    add = 0;
    value = value.replace("-","");
    value = value.replace(/\./g,"");

    for( var j=0; j < 9; j++ ) {
      add += parseInt(value.charAt(j),10) * (10-j);
    }

    rev = 11 - ( add % 11 );

    if( rev == 10 || rev == 11) {
      rev = 0;
    }

    if( rev != parseInt(value.charAt(9),10) ) {
      result = false;
    }

    add = 0;

    for( var k=0; k < 10; k++ ) {
      add += parseInt(value.charAt(k),10) * (11-k);
    }

    rev = 11 - ( add % 11 );

    if( rev == 10 || rev == 11) {
      rev = 0;
    }

    if( rev != parseInt(value.charAt(10),10) ) {
      result = false;
    }

    if ( result ) {
        input.parentNode.previousSibling.previousSibling.classList.remove('hide'); 
        input.parentNode.nextSibling.nextSibling.classList.add('hide'); 
        input.classList.remove('form_invalido');                
        input.classList.add('form_valido');     
    } else {
        input.parentNode.previousSibling.previousSibling.classList.add('hide'); 
        input.parentNode.nextSibling.nextSibling.classList.remove('hide'); 
        input.classList.add('form_invalido');               
        input.classList.remove('form_valido');      
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 2014-04-09
    • 1970-01-01
    • 2019-05-21
    相关资源
    最近更新 更多