1
I tried to create a simple mask for my form, however the landline is not returning as expected.
Code:
function formatar(mascara, documento){
var i = documento.value.length;
var saida = mascara.substring(0,1);
var texto = mascara.substring(i)
if (texto.substring(0,1) != saida){
documento.value += texto.substring(0,1);
}
}
<label for="Cep"> CEP: </label>
<input type="text" size="8" id="Cep" maxlength="9" OnKeyPress="formatar('#####-###', this)">
<br>
<label id="data">Data de Nascimento:</label>
<label for="Cdata"></label>
<input type="text" name="data" id="data" size="9" maxlength="10" OnKeyPress="formatar('##/##/####', this)">
<br>
<label for="Cpf"> CPF:</label>
<input type="text" id="Cpf" size="12" maxlength="14" OnKeyPress="formatar('###.###.###-##', this)"/>
<br>
<label for="Crg"> RG:</label>
<input type="text" name="Trg" id="Crg" size="12" maxlength="14" OnKeyPress="formatar('##.###.###-##', this)">
<br>
<label for="TelF"> Tel Fixo</label>
<input type="text" name="fTel" id="TelF" size="14" maxlength="14" OnKeyPress="formatar('(##) ####-####', this)">
Upshot:
have tried to put a Return in this function, I’m giving a kick, I do not know if the object there is passed by reference or not.
– Nicola Bogar
Even with Return, it didn’t work.
– Paulo Xavier
Check if there is an error in the browser console?
– Nicola Bogar
The problem is the condition, the logic is to check if the element is different from the element of the mask and add the previous one in value, but the previous one does not exist, this is breaking the logic.
– arllondias
No error on console.
– Paulo Xavier
put 0 in place of #
– Victor
You can even do it this way you are trying, but you have already checked the possibility of using a Jquerymask plugin is very good and more practical. https://igorescobar.github.io/jQuery-Mask-Plugin/docs.html
– arllondias
Possible duplicate of Change phone mask
– NoobSaibot
@Pauloxavier Note: you should mark the answer that best served you
– Victor