[Resolvido] Problema em formatar CNPJ
estou tentando fazer um formulário q vá formatando o q o usuário vai digitando.
alguns campos funcionam beleza mas o cnpj não.
é difícil de explicar o q acontece, copiem essa parte do código e testem aí rapidinho q vocês vão ver na hora!!
reparei q este problema acontece quando tenho 3 objetos seguidos, (3 .) ou (3 -) ou (3 /), como no cnpj tem 3 pontos e só depois um ífem (99.999.999.9999-99)
ajudem aí galera!!!
agradeço
<script language="javascript">
function txtBoxFormat(objeto, 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 {
nTecla = evtKeyPress.which;
if (nTecla == 8) {
return true;
}
}
sValue = objeto.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( ")", "" );
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) == "/") || (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++;
}
objeto.value = sCod;
if (nTecla != 8) { // backspace
if (sMask.charAt(i-1) == "9") { // apenas números...
return ((nTecla > 47) && (nTecla < 58)); }
else { // qualquer caracter...
return true;
}
}
else {
return true;
}
}
</script>
<form action="cadastrando cliente.php" method="post" name="cadastrocliente">
<input type="text" name="telefone" onKeyPress="return txtBoxFormat(this, '(99)9999-9999', event);" maxlength="13" size="25" />
<br>
<input type="text" name="celular" onKeyPress="return txtBoxFormat(this, '(99)9999-9999', event);" maxlength="13" size="25" />
<br>
<input type="text" name="cpf" onKeyPress="return txtBoxFormat(this, '999.999.999-99', event);" maxlength="14" size="25" />
<br>
<input type="text" name="cnpj" onKeyPress="return txtBoxFormat(this, '99.999.999.9999-99', event);" maxlength="18" size="25" />
<br>
</form>Discussão (8)
Carregando comentários...