fiz algumas máscaras... segue o exemplo completo:
<script>
function mascara_data(CAMPO) {
valor = CAMPO.value.length;
if(valor == 2 || valor == 5) { CAMPO.value += '/'; }
}
function mascara_cpf(CAMPO) {
valor = CAMPO.value.length;
if(valor == 3 || valor == 7) { CAMPO.value += '.'; }
if(valor == 11) { CAMPO.value += '/'; }
}
function mascara_cep(CAMPO) {
if(CAMPO.value.length == 5) { CAMPO.value += '-'; }
}
function mascara_telefone(CAMPO) {
valor = CAMPO.value.length;
if(valor == 2) { CAMPO.value += ' '; }
if(valor == 7) { CAMPO.value += '-'; }
}
function mascara_cnpj(CAMPO) {
valor = CAMPO.value.length;
if(valor == 2 || valor ==6) { CAMPO.value += '.'; }
if(valor == 10) { CAMPO.value += '/'; }
if(valor == 15) { CAMPO.value += '-'; }
}
</script>
<form name="form">
Data: <input type="text" name="data" onKeyUp="return mascara_data(this)" maxlength=10><br>
CPF: <input type="text" name="cpf" onKeyUp="return mascara_cpf(this)" maxlength=14><br>
CNPJ: <input type="text" name="cnpj" onKeyUp="return mascara_cnpj(this)" maxlength=18><br>
CEP: <input type="text" name="cep" onKeyUp="return mascara_cep(this)" maxlength=9><br>
Telefone: <input type="text" name="telefone" onKeyUp="return mascara_telefone(this)" maxlength=12><br>
</form>Ao testar, vocês podem perceber que se errarem um valor, para apagar via "backspace" é uma xxxxxx, pois o script insiste em completar o campo.
Dei uma olhada nessa máscara de CEP disponibilizada aqui no fórum:
<script>
function MM_formtCep(e,src,mask) {
if(window.event) { _TXT = e.keyCode; }
else if(e.which) { _TXT = e.which; }
if(_TXT > 47 && _TXT < 58) {
var i = src.value.length; var saida = mask.substring(0,1); var texto = mask.substring(i)
if (texto.substring(0,1) != saida) { src.value += texto.substring(0,1); }
return true; } else { if (_TXT != 8) { return false; }
else { return true; }
}
}
</script>
<input name="cep" type="text" id="cep" onkeypress="return MM_formtCep(event,this,'#####-###');" size="10" maxlength="9">e nela não tem esse problema ao tentar corrigir o valor...
ainda estou tentando captar a "escencia" para modificar...
enquanto isso, alguém pode me dar um help?










