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

Erro Formatar Campo Javascript


tainá

Pergunta

Bom dia pessoal, preciso de uma ajuda...

Estou usando uma função para formatar campos, mas no Firefox, quando

Eu tento apagar os caracteres digitados, a função impede...

Alguém pode ajudar?

Essa é a função:

<html>
<head>
<title> Textbox formatado... </title>
 <script language="JavaScript">
   function txtBoxFormat(objForm, strField, sMask, evtKeyPress) {
     var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

     if(document.all) { // Internet Explorer
       nTecla = evtKeyPress.keyCode; }
     else if(document.layers) { // Nestcape
       nTecla = evtKeyPress.which; }
     else if(document.getElementsByTagName("input")){//Firefox
       nTecla = evtKeyPress.which;
     }

     sValue = objForm[strField].value;
     // Limpa todos os caracteres de formatação que
     // já estiverem no campo.
     sValue = sValue.toString().replace( "-", "" );
     sValue = sValue.toString().replace( "-", "" );
     sValue = sValue.toString().replace( ".", "" );
     sValue = sValue.toString().replace( ".", "" );
     sValue = sValue.toString().replace( "/", "" );
     sValue = sValue.toString().replace( "/", "" );
     sValue = sValue.toString().replace( "(", "" );
     sValue = sValue.toString().replace( "(", "" );
     sValue = sValue.toString().replace( ")", "" );
     sValue = sValue.toString().replace( ")", "" );
     sValue = sValue.toString().replace( " ", "" );
     sValue = sValue.toString().replace( " ", "" );
     fldLen = sValue.length;
     mskLen = sMask.length;

     i = 0;
     nCount = 0;
     sCod = "";
     mskLen = fldLen;

     while (i <= mskLen) {
       bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
       bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

       if (bolMask) {
         sCod += sMask.charAt(i);
         mskLen++; }
       else {
         sCod += sValue.charAt(nCount);
         nCount++;
       }

       i++;
     }

     objForm[strField].value = sCod;

     if (nTecla != 8) { // backspace
       if (sMask.charAt(i-1) == "9") { // apenas números...
         return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9
       else { // qualquer caracter...
         return true;
       } }
     else {
       return true;
     }
   }
 </script>
</head>
<body>
<form name="myForm">
<table border="0" cellspacing="2" cellpadding="2">
 <tr>
   <td>cep &nbsp;</td>
   <td>
     <input type="text"
            name="str_cep"
            maxlength="09"
            size="10"
            onkeypress="return txtBoxFormat(document.myForm, 'str_cep', '99999-999', event);"></td>
 </tr>
 <tr>
   <td>cpf &nbsp;</td>
   <td>
     <input type="text"
            name="str_cpf"
            maxlength="14"
            size="20"
            onkeypress="return txtBoxFormat(document.myForm, 'str_cpf', '999.999.999-99', event);">
   </td>
 </tr>
 <tr>
   <td>tel &nbsp;</td>
   <td>
     <input type="text"
            name="str_tel"
            maxlength="14"
            size="20"
            onkeypress="return txtBoxFormat(document.myForm, 'str_tel', '(99) 9999-9999', event);">
   </td>
 </tr>
 <tr>
 <td>conta &nbsp;</td>
 <td>
   <input type="text"
          name="str_cc"
          maxlength="6"
          size="10"
          onkeypress="return txtBoxFormat(document.myForm, 'str_cc', '9999-!', event);">
   </td>
 </tr>
</table>
</form>
</body>
</html>

Link para o comentário
Compartilhar em outros sites

0 respostass a esta questão

Posts Recomendados

Até agora não há respostas para essa pergunta

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
      152k
    • Posts
      651,8k
×
×
  • Criar Novo...