Ir para conteúdo
Fórum Script Brasil
  • 0

Validação E Mascara No Mesmo Programa


DataSet

Pergunta

Tenho um programinha javascript que valida cpf.. tudo beleza...

junto com ele... coloquei um codigo que formata com . e - criando a mascara...

mas agora ele não passa na validação... pois ele pega o campo todo...

Como eu faço pra arrumar? já que meu forte não é javascript... peço ajuda dos feras...

Valeu

<html>

<head>

<script language="Javascript">

function validaCPF() {

  cpf = document.validacao.cpfID.value;

  valor = true;

  erro = new String;

  if (cpf.length < 11) erro += "são necessarios 11 digitos para verificacao do CPF!";

  var nonNumbers = /\D/;

  if (nonNumbers.test(cpf)) erro += "A verificacao de CPF suporta apenas numeros! \n\n";

  if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){

    erro += "Numero de CPF invalido!"

  }

  var a = [];

  var b = new Number;

  var c = 11;

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

  a = cpf.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.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){

  erro +="Digito verificador com problema!";

  }

  if (erro.length > 0){

  alert(erro);

  return false;

  }

  return true;

}

// Início da Marcara do CPF

function FormataCpf(tammax) {

var tecla = event.keyCode;

vr = event.srcElement.value;

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

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

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

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

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

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

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

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

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

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

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

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

tam = vr.length;

if (tam < tammax && tecla != 8){ tam = vr.length + 1; }

if (tecla == 8 ){ tam = tam - 1; }

if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){

if ( tam <= 2 ){

  event.srcElement.value = vr; }

  if ( (tam > 2) && (tam <= 5) ){

  event.srcElement.value = vr.substr( 0, tam - 2 ) + '-' + vr.substr( tam - 2, tam ); }

  if ( (tam >= 6) && (tam <= 8) ){

  event.srcElement.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam ); }

  if ( (tam >= 9) && (tam <= 11) ){

  event.srcElement.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam ); }

  if ( (tam >= 12) && (tam <= 14) ){

  event.srcElement.value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam ); }

  if ( (tam >= 15) && (tam <= 17) ){

  event.srcElement.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam );}

}

}

// Fim da Máscara do CPF

</script>

</head>

<body leftmargin="10" topmargin="10" marginwidth="10" marginheight="10">

<form action="validaCPF.htm" name="validacao" onSubmit="return validaCPF()" onKeyPress="FormataCpf(11)">

<input type="text" name="cpfID" id="cpfID" maxlength="14" value="">

<input name="Submit" type="submit" value="enviar">

</form>

</body>

</html>

Aí está o codigo... salve como validaCPF.htm

Valeu...

Link para o comentário
Compartilhar em outros sites

2 respostass a esta questão

Posts Recomendados

  • 0

Consegui... usei o charAt e um for para contar meus campos....

Para aqueles que se interessaram pelo meu problema... obrigadão...

valeu

Até

<html>

<head>

<script language="Javascript">

function validaCPF() {

  cpf = document.validacao.cpfID.value;

    numeros = "0123456789"

    cpfx = "";

    for (x = 0; x <= 14; x++)

    {

    if (numeros.indexOf(cpf.charAt(x)) >= 0)

      cpfx = cpfx + cpf.charAt(x);

    }

  alert (cpfx);

  valor = true;

  erro = new String;

  if (cpf.length < 11) erro += "são necessarios 11 digitos para verificacao do CPF!";

  var nonNumbers = /\D/;

  if (nonNumbers.test(cpfx)) erro += "A verificacao de CPF suporta apenas numeros!";

  if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){

    erro += "Numero de CPF invalido!"

  }

  var a = [];

  var b = new Number;

  var c = 11;

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

  a = cpfx.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 ((cpfx.charAt(9) != a[9]) || (cpfx.charAt(10) != a[10])){

  erro +="Digito verificador com problema!";

  }

  if (erro.length > 0){

  alert(erro);

  return false;

  }

  return true;

}

// Início da Marcara do CPF

function FormataCpf(tammax) {

var tecla = event.keyCode;

vr = event.srcElement.value;

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

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

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

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

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

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

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

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

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

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

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

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

tam = vr.length;

if (tam < tammax && tecla != 8){ tam = vr.length + 1; }

if (tecla == 8 ){ tam = tam - 1; }

if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){

if ( tam <= 2 ){

  event.srcElement.value = vr; }

  if ( (tam > 2) && (tam <= 5) ){

  event.srcElement.value = vr.substr( 0, tam - 2 ) + '-' + vr.substr( tam - 2, tam ); }

  if ( (tam >= 6) && (tam <= 8) ){

  event.srcElement.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam ); }

  if ( (tam >= 9) && (tam <= 11) ){

  event.srcElement.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam ); }

  if ( (tam >= 12) && (tam <= 14) ){

  event.srcElement.value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam ); }

  if ( (tam >= 15) && (tam <= 17) ){

  event.srcElement.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + '-' + vr.substr( tam - 2, tam );}

}

}

// Fim da Máscara do CPF

</script>

</head>

<body leftmargin="10" topmargin="10" marginwidth="10" marginheight="10">

<form action="validaCPF.htm" name="validacao" onSubmit="return validaCPF()" onKeyPress="FormataCpf(11)">

<input type="text" name="cpfID" id="cpfID" maxlength="14" value="">

<input name="Submit" type="submit" value="enviar">

</form>

</body>

</html>

Link para o comentário
Compartilhar em outros sites

  • 0

dei uma mexida no teu código... agora tah tudo funcionando...(Eu acho, não testei no IE).

<html>
<head>
<script language="Javascript">
function validaCPF() {
  cpf = document.validacao.cpfID.value;
  cpf = Limp(cpf);
  valor = true;
  erro = new String;
  if (cpf.length < 11) erro += "são necessarios 11 digitos para verificacao do CPF!";
  var nonNumbers = /\D/;
  if (nonNumbers.test(cpf)) erro += "A verificacao de CPF suporta apenas numeros! \n\n";
  if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
    erro += "Numero de CPF invalido!"
  }
  var a = [];
  var b = new Number;
  var c = 11;
  for (i=0; i<11; i++){
  a[i] = cpf.charAt(i);
  if (i < 9) b += (a[i] *  --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.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
  erro +="Digito verificador com problema!";
  }
  if (erro.length > 0){
  alert(erro);
  return false;
  }
  return true;
}

// Início da Marcara do CPF
function FormataCpf(evt) {
	vr = (navigator.appName == 'Netscape') ?evt.target.value : evt.srcElement.value;
	if(vr.length == 3) vr = vr+".";
    if(vr.length == 7) vr = vr+".";
    if(vr.length == 11) vr = vr+"-";
    return vr;
}
// Fim da Máscara do CPF
//função para Limpar e deixar somente os números.
function Limp(c){
	qtd = c.length;
	var v = '';
	for (i=0; i < qtd; i++)
  for(t=0; t < 10; t++){
  	if(c.substring(i,i+1) == t && c.substring(i,i+1) != " ") v += c.substring(i,i+1);}
	return(v);
}

</script>
</head>
<body leftmargin="10" topmargin="10" marginwidth="10" marginheight="10">
<form action="validaCPF.htm" name="validacao" onSubmit="return ">
<input type="text" name="cpfID" id="cpfID" maxlength="14" value="" onChange="validaCPF()" onKeyPress="this.value = FormataCpf(event)">
<input name="Submit" type="submit" value="enviar">
</form>
</body>
</html>

Qualquer coisa posta ae...

Abraço

Crystian

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,2k
    • Posts
      651,9k
×
×
  • Criar Novo...