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

Mascara


PERNAMBUCO_FJV

Pergunta

4 respostass a esta questão

Posts Recomendados

  • 0

<html>
<head>
</head>

<body>
<p> 
  <script>
function Limpar(valor, validos) {
// retira caracteres invalidos da string
var result = "";
var aux;
for (var i=0; i < valor.length; i++) {
aux = validos.indexOf(valor.substring(i, i+1));
if (aux>=0) {
result += aux;
}
}
return result;
}

//Formata número tipo moeda usando o evento onKeyDown

function Formata(campo,tammax,teclapres,decimal) {
var tecla = teclapres.keyCode;
vr = Limpar(campo.value,"0123456789");
tam = vr.length;
dec=decimal

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 <= dec )
{ campo.value = vr; }

if ( (tam > dec) && (tam <= 5) ){
campo.value = vr.substr( 0, tam - 2 ) + "." + vr.substr( tam - dec, tam ); }
if ( (tam >= 6) && (tam <= 8) ){
campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "." + vr.substr( tam - dec, tam ); 
}
if ( (tam >= 9) && (tam <= 11) ){
campo.value = vr.substr( 0, tam - 8 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "." + vr.substr( tam - dec, tam ); }
if ( (tam >= 12) && (tam <= 14) ){
campo.value = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "." + vr.substr( tam - dec, tam ); }
if ( (tam >= 15) && (tam <= 17) ){
campo.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 );}
} 

}

</script>
  <input name="T1" type="text" onKeydown="Formata(this,20,event,2)" size="20" maxlength="9"></p> </p> 
</p>
</body>

</html> 

excelente código!!

mas tem um problema:

Eu não entendi ele!! sad.gif

ae eu queria que ficasse no formato

111 | 1.111 | 11.111

e não

1.11 | 11.11 | 111.11

ee eu não sei alterar

alguém pode me explicar como eu altero?? e onde?

Link para o comentário
Compartilhar em outros sites

  • 0

Tem um outro script que o jissa postou... acho que tá melhor:

<html>

<head>

<title> Textbox formatado... </title>

<script language="JavaScript">

  /***

  * Descrição.: formata um campo do formulário de

  * acordo com a máscara informada...

  * Parâmetros: - objForm (o Objeto Form)

  * - strField (string contendo o nome

  * do textbox)

  * - sMask (mascara que define o

  * formato que o dado será apresentado,

  * usando o algarismo "9" para

  * definir números e o símbolo "!" para

  * qualquer caracter...

  * - evtKeyPress (evento)

  *

  *

  *

  *

  * Uso.......: <input type="textbox"

  * name="xxx".....

  * onkeypress="return txtBoxFormat(document.rcfDownload, 'str_cep', '99999-999', event);">

  * Observação: As máscaras podem ser representadas

  * como os exemplos abaixo:

  * CEP -> 99999-999

  * CPF -> 999.999.999-99

  * CNPJ -> 99.999.999/9999-99

  * C/C -> 999999-!

  * Tel -> (99) 9999-9999

  ***/

  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;

    }

    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>

Com este é mais fácil smile.gif

Falou!

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