Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Ilano

Validar Cpf - Qual O Erro Deste Javascript

Recommended Posts

Olá pessoal,

 

Peguei o javascript abaixo e tentei aplicá-lo em minha página, porém, só valida o CPF se eu não digitar pontos e traço, mesmo ele tratando esse problema usando replace.

 

Por favor, o que está errado neste código?

 

Grato,

 

Ilano.

 

//VALIDAR CPF:

//MODO DE USAR: onkeyup="javascript:maskCPF(this);" onblur="javascript:validaCPF(this);"

 

function validaCPF(cpf)

{

erro = new String;

 

if (cpf.value.length == 11)

{

cpf.value = cpf.value.replace('.', '');

cpf.value = cpf.value.replace('.', '');

cpf.value = cpf.value.replace('-', '');

 

var nonNumbers = /\D/;

 

if (nonNumbers.test(cpf.value))

{

erro = "A verificacao de CPF suporta apenas números!";

}

else

{

if (cpf.value == "00000000000" ||

cpf.value == "11111111111" ||

cpf.value == "22222222222" ||

cpf.value == "33333333333" ||

cpf.value == "44444444444" ||

cpf.value == "55555555555" ||

cpf.value == "66666666666" ||

cpf.value == "77777777777" ||

cpf.value == "88888888888" ||

cpf.value == "99999999999") {

 

erro = "Número de CPF inválido!"

}

 

var a = [];

var b = new Number;

var c = 11;

 

for (i=0; i<11; i++){

a = cpf.value.charAt(i);

if (i < 9) b += (a * --c);

}

 

if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }

b = 0;

c = 11;

 

for (y=0; y<10; y++) b += (a[y] * c--);

 

if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }

 

if ((cpf.value.charAt(9) != a[9]) || (cpf.value.charAt(10) != a[10])) {

erro = "Número de CPF inválido.";

}

}

}

else

{

if(cpf.value.length == 0)

return false

else

erro = "Número de CPF inválido.";

}

if (erro.length > 0) {

alert(erro);

cpf.focus();

return false;

}

return true;

}

 

//envento onkeyup

function maskCPF(CPF) {

var evt = window.event;

kcode=evt.keyCode;

if (kcode == 8) return;

if (CPF.value.length == 3) { CPF.value = CPF.value + '.'; }

if (CPF.value.length == 7) { CPF.value = CPF.value + '.'; }

if (CPF.value.length == 11) { CPF.value = CPF.value + '-'; }

}

 

// evento onblur

function formataCPF(CPF)

{

with (CPF)

{

value = value.substr(0, 3) + '.' +

value.substr(3, 3) + '.' +

value.substr(6, 3) + '-' +

value.substr(9, 2);

}

}

function retiraFormatacao(CPF)

{

with (CPF)

{

value = value.replace (".","");

value = value.replace (".","");

value = value.replace ("-","");

value = value.replace ("/","");

}

}

Compartilhar este post


Link para o post
Compartilhar em outros sites

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.