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

Travando Formulários


Guest - SirSmart -

Pergunta

5 respostass a esta questão

Posts Recomendados

  • 0
Guest - Marcos -

function mascara(oque,tipo,tamanho1,tamanho2,sinal)

{

// Descrição: Máscaras para edição de campos de formulário.

// usar sinal = 1 para valores positivos/negativos. tamanho1 e tamanho2 são opcionais e determinam o tamanho máximo de um campo numérico e suas casas decimais.

// tipos: cep,cpf,cgc,cnpj10,ddd,ramal,fone,celular,DD/MM/AA,DD/MM/AAAA,MM/AAAA,IE,caracter,numero,valor,percentual,cartao,cc,poupanca,unidade

// Exemplo: onKeyUp="mascara(this,'cep');"

// Exemplo: onKeyUp="mascara(this,'valor',13,2);"

// Exemplo: onKeyUp="mascara(this,'valor',13,2,1);"

if( (event.keyCode == 8) || (event.keyCode == 13) || (event.keyCode == 37) || (event.keyCode == 39) || (event.keyCode == 46) || (event.keyCode == 16))

{

return;

}

if(tamanho1 != null) {tamanho1 = parseInt(tamanho1);}

if(tamanho2 != null) {tamanho2 = parseInt(tamanho2);}

retorno = '';

switch (tipo) {

case 'cep':

{ // 99999-999

oque.maxLength=9;

retorno = limpaParaMascara(oque.value,'numeros');

retorno = retorno.substr(0,9);

if(retorno.length >= 8 && (retorno-0 == 0) ) {

alert(retorno.substr(0,5)+"-"+retorno.substr(5,7)+"\n"+"CEP inválido.");

oque.value="";

oque.focus();

return;

}

if(retorno.length >= 5) { retorno = retorno.substr(0,5)+"-"+retorno.substr(5,7); }

oque.value = retorno.substr(0,9);

break;

}

case 'cpf':

{ // 999.999.999-99

retorno = limpaParaMascara(oque.value,'numeros');

if (retorno.length < 12) {

oque.maxLength=14;

if(retorno.length >= 3) { retorno = retorno.substr(0,3) +"."+retorno.substr(3) ;}

if(retorno.length >= 7) { retorno = retorno.substr(0,7) +"."+retorno.substr(7) ;}

if(retorno.length >= 11) { retorno = retorno.substr(0,11)+"-"+retorno.substr(11);}

oque.value = retorno.substr(0,14);

if(retorno.length >= 14) {

if(!validaCPF(retorno))

{

alert(oque.value+"\n"+"CPF inválido.");

oque.value="";

oque.focus();

return;

}

}

} else {

oque.maxLength=18;

if(retorno.length >= 2) { retorno = retorno.substr(0,2)+"."+retorno.substr(2); }

if(retorno.length >= 6) { retorno = retorno.substr(0,6)+"."+retorno.substr(6); }

if(retorno.length >= 10) { retorno = retorno.substr(0,10)+"/"+retorno.substr(10); }

if(retorno.length >= 15) { retorno = retorno.substr(0,15)+"-"+retorno.substr(15); }

oque.value = retorno.substr(0,18);

if(retorno.length >= 18)

{

if(!validaCGC(retorno))

{

alert(oque.value+"\n"+"CNPJ inválido.");

//oque.value="";

oque.focus();

return;

}

}

}

break;

}

case 'cgc':

{ // 99.999.999/9999-99

retorno = limpaParaMascara(oque.value,'numeros');

if (retorno.length < 12) {

oque.maxLength=14;

if(retorno.length >= 3) { retorno = retorno.substr(0,3) +"."+retorno.substr(3) ;}

if(retorno.length >= 7) { retorno = retorno.substr(0,7) +"."+retorno.substr(7) ;}

if(retorno.length >= 11) { retorno = retorno.substr(0,11)+"-"+retorno.substr(11);}

oque.value = retorno.substr(0,14);

if(retorno.length >= 14) {

if(!validaCPF(retorno))

{

alert(oque.value+"\n"+"CPF inválido.");

oque.value="";

oque.focus();

return;

}

}

} else {

oque.maxLength=18;

if(retorno.length >= 2) { retorno = retorno.substr(0,2)+"."+retorno.substr(2); }

if(retorno.length >= 6) { retorno = retorno.substr(0,6)+"."+retorno.substr(6); }

if(retorno.length >= 10) { retorno = retorno.substr(0,10)+"/"+retorno.substr(10); }

if(retorno.length >= 15) { retorno = retorno.substr(0,15)+"-"+retorno.substr(15); }

oque.value = retorno.substr(0,18);

if(retorno.length >= 18)

{

if(!validaCGC(retorno))

{

alert(oque.value+"\n"+"CNPJ inválido.");

//oque.value="";

oque.focus();

return;

}

}

}

break;

}

case 'cnpj10':

{ // 999.999.99

oque.maxLength=10;

retorno = limpaParaMascara(oque.value,'numeros');

if(retorno.length >= 2) { retorno = retorno.substr(0,2)+"."+retorno.substr(2); }

if(retorno.length >= 6) { retorno = retorno.substr(0,6)+"."+retorno.substr(6); }

oque.value = retorno.substr(0,10);

break;

}

case 'ddd': { // 9999

oque.maxLength=4;

retorno = limpaParaMascara(oque.value,'numeros');

retorno = retorno.substr(0,4);

if(retorno.length >= 4 && (retorno-0 == 0) ) {

alert("O DDD não pode ser "+retorno+".");

oque.value="";

oque.focus();

return;

}

oque.value = retorno.substr(0,4);

break; }

case 'ramal': { // 9999

oque.maxLength=4;

retorno = limpaParaMascara(oque.value,'numeros');

oque.value = retorno.substr(0,4);

break; }

case 'fone': { // 9999999999

oque.maxLength=10;

retorno = limpaParaMascara(oque.value,'numeros');

//if(retorno.length >= 3) { retorno = retorno.substr(0,3)+"."+retorno.substr(3); }

oque.value = retorno.substr(0,10);

break; }

case 'celular': { // 9999999999

oque.maxLength=10;

retorno = limpaParaMascara(oque.value,'numeros');

//if(retorno.length >= 4) { retorno = retorno.substr(0,4)+"."+retorno.substr(4); }

oque.value = retorno.substr(0,10);

break; }

case 'DD/MM/AA':

{ // 99/99/99

alert ('entrei');

oque.maxLength=8;

retorno = limpaParaMascara(oque.value,'numeros');

if(retorno.length >= 2) { retorno = retorno.substr(0,2)+"/"+retorno.substr(2); }

if(retorno.length >= 5) { retorno = retorno.substr(0,5)+"/"+retorno.substr(5); }

oque.value = retorno.substr(0,8);

if(retorno.length >= 8)

{

dataEmTeste = retorno.substr(0,6)+'20'+retorno.substr(6,2) ;

if(!retornaValidaData(dataEmTeste))

{

alert('Data inválida.')

oque.value='';

oque.focus();

oque.select();

return;

}

}

break;

}

case 'DD/MM/AAAA':

{ // 99/99/9999

oque.maxLength=10;

retorno = limpaParaMascara(oque.value,'numeros');

if(retorno.length >= 2)

{

retorno = retorno.substr(0,2)+"/"+retorno.substr(2);

}

if(retorno.length >= 5)

{

retorno = retorno.substr(0,5)+"/"+retorno.substr(5);

}

oque.value = retorno.substr(0,10);

if(retorno.length >= 10)

{

if(!retornaValidaData(oque.value))

{

oque.value='';

alert('Data inválida.')

oque.focus();

oque.select();

return;

}

}

break;

}

case 'MM/AAAA': { // 99/9999

oque.maxLength=7;

retorno = limpaParaMascara(oque.value,'numeros');

if(retorno.length >= 2) { retorno = retorno.substr(0,2)+"/"+retorno.substr(2); }

oque.value = retorno.substr(0,7);

if(retorno.length >= 7) {

dataEmTeste = '01/'+retorno

if(!retornaValidaData(dataEmTeste)) {

oque.value='';

oque.focus();

oque.select();

return;

}

}

break; }

case 'IE':

{ // AAAAAAAAAAAAAA

oque.maxLength=14;

//retorno = limpaParaMascara(oque.value,'numeros');

//if(retorno.length >= 3) { retorno = retorno.substr(0,3)+"/"+retorno.substr(3); }

//oque.value = retorno ;

break;

}

case 'caracter':

{

break;

}

case 'numero':

{

if(tamanho1 != null)

oque.maxLength = tamanho1;

retorno = limpaParaMascara(oque.value,'numeros');

oque.value = retorno.substr(0,oque.maxLength);

break;

}

case 'valor':

{

if( (tamanho1 != null) && (tamanho2 != null) )

{

if(tamanho2 == 0)

{

oque.maxLength = tamanho1;

}

else

{

oque.maxLength = tamanho1+1+tamanho2;

}

}

testaValor(oque, tamanho2)

break;

}

case 'percentual':

{

if( (tamanho1 != null) && (tamanho2 != null) )

{

if(tamanho2 == 0)

{

oque.maxLength = tamanho1;

}

else

{

oque.maxLength = tamanho1+1+tamanho2;

}

}

testaValor(oque, tamanho2)

break;

}

case 'cartao': { // 9999 9999 9999 9999

oque.maxLength=19;

retorno = limpaParaMascara(oque.value,'numeros');

if(retorno.length >= 4) { retorno = retorno.substr(0,4)+" "+retorno.substr(4); }

if(retorno.length >= 9) { retorno = retorno.substr(0,9)+" "+retorno.substr(9); }

if(retorno.length >= 14) { retorno = retorno.substr(0,14)+" "+retorno.substr(14); }

oque.value = retorno.substr(0,19);

break; }

case 'cc': { // 9999-99999-99

oque.maxLength=13;

retorno = limpaParaMascara(oque.value,"numeros");

if(retorno.length >= 4) { retorno = retorno.substr(0,4) + "-" + retorno.substr(4); }

if(retorno.length >= 10) { retorno = retorno.substr(0,10) + "-" + retorno.substr(10); }

oque.value = retorno.substr(0,13);

break; }

case 'poupanca': { // 9999-999999-9

oque.maxLength=13;

retorno = limpaParaMascara(oque.value,"numeros");

if(retorno.length >= 4) { retorno = retorno.substr(0,4) + "-" + retorno.substr(4); }

//alert(retorno);

if(retorno.length >= 11) { retorno = retorno.substr(0,11) + "-" + retorno.substr(11); }

oque.value = retorno.substr(0,13);

break; }

case 'unidade': { // 99999-99

oque.maxLength=8;

retorno = limpaParaMascara(oque.value,"numeros");

if(retorno.length >= 5) { retorno = retorno.substr(0,5) + "-" + retorno.substr(5); }

oque.value = retorno.substr(0,8);

break; }

case 'DD': { // 99

oque.maxLength=2;

retorno = limpaParaMascara(oque.value,"numeros");

if(retorno.length > 2) { retorno = retorno.substr(0,2);}

if (retorno > 31 || retorno < 1){ alert('Dia Inválido.'); oque.focus(); oque.select();return;}

oque.value = retorno;

break; }

case 'MM': { // 99

oque.maxLength=2;

retorno = limpaParaMascara(oque.value,"numeros");

if(retorno.length > 2) { retorno = retorno.substr(0,2);}

if (retorno > 12 || retorno < 1){ alert('Mês Inválido.'); oque.focus(); oque.select();return;}

oque.value = retorno;

break; }

case 'AAAA': { // 9999

oque.maxLength=4;

retorno = limpaParaMascara(oque.value,"numeros");

if(retorno.length > 4) { retorno = retorno.substr(0,4);}

if (retorno < 1900 || retorno >2040){ alert('Ano Inválido.'); oque.focus(); oque.select();return;}

oque.value = retorno;

break; }

}

}

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...