Jump to content
Fórum Script Brasil
  • 0

Máascara Em Input's


myers
 Share

Question

4 answers to this question

Recommended Posts

  • 0

Tenta esse...

<html> 
<head> 
<title>Máscaras para Formulários</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 -> 99.999-999 
* CPF -> 999.999.999-99 
* CNPJ -> 99.999.999/9999-99 
* Data -> 99/99/9999 
* Tel Resid -> (99) 999-9999 
* Tel Cel -> (99) 9999-9999 
* Processo -> 99.999999999/999-99 
* C/C -> 999999-! 
* E por aí vai... 
***/

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;
}
}
//Fim da Função Máscaras Gerais
</script>
</head> 
<body> 
<font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>Formatação 
de várias Máscaras de Entradas para campos de Formulários</strong></font> 
<form name="Form"> 
<table border="0" cellspacing="2" cellpadding="2"> 
<tr> 
<td width="66"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Cep 
&nbsp;</font></td> 
<td width="128"> <input type="text" 
name="str_cep" 
maxlength="10" 
size="10" 
onKeyPress="return txtBoxFormat(document.Form, 'str_cep', '99.999-999', event);"> 
</td> 
</tr> 
<tr> 
<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">CPF</font></td> 
<td> <input type="text" 
name="str_cpf" 
maxlength="14" 
size="14" 
onKeyPress="return txtBoxFormat(document.Form, 'str_cpf', '999.999.999 99', event);"> 
</td> 
</tr> 
<tr> 
<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">CNPJ</font></td> 
<td><input type="text" 
name="str_cnpj" 
maxlength="18" 
size="18" 
onKeyPress="return txtBoxFormat(document.Form, 'str_cnpj', '99.999.999/9999-99', event);"></td> 
</tr> 
<tr> 
<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Data</font></td> 
<td><input type="text" 
name="str_data" 
maxlength="10" 
size="10" 
onKeyPress="return txtBoxFormat(document.Form, 'str_data', '99/99/9999', event);"></td> 
</tr> 
<tr> 
<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Tel Residencial</font></td> 
<td><input type="text" 
name="str_tel2" 
maxlength="14" 
size="14" 
onKeyPress="return txtBoxFormat(document.Form, 'str_tel2', '(99) 9999-9999', event);"></td> 
</tr> 
<tr> 
<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Tel Celular&nbsp;</font></td> 
<td> <input type="text" 
name="str_tel" 
maxlength="14" 
size="14" 
onKeyPress="return txtBoxFormat(document.Form, 'str_tel', '(99) 9999-9999', event);"> 
</td> 
</tr> 
<tr> 
<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Processo</font></td> 
<td><input type="text" 
name="str_proc" 
maxlength="20" 
size="20" 
onKeyPress="return txtBoxFormat(document.Form, 'str_proc', '99.999999999/9999-99', event);"></td> 
</tr> 
<tr> 
<td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Conta &nbsp;</font></td> 
<td> <input type="text" 
name="str_cc" 
maxlength="6" 
size="10" 
onKeyPress="return txtBoxFormat(document.Form, 'str_cc', '9999-!', event);"> 
</td> 
</tr> 
</table> 
</form> 
</body> 
</html>

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share



  • Forum Statistics

    • Total Topics
      150.7k
    • Total Posts
      648.4k
×
×
  • Create New...