Input Mask not working in mobile version

Asked

Viewed 157 times

0

I have a very simplistic code to create masks in input fields It works that is a beauty on the desktop but when I run the script on mobile simply nothing happens, because?

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);
  }
  
}
.tabela {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 10px;
}
<table width="301" height="160" border="0" cellspacing="0" class="tabela">
  <tr>
    <td height="34" colspan="2" valign="top">Este script formata qualquer tipo de campo.<br>
Veja o Exemplo abaixo</td>
  </tr>
  <tr>
    <td width="32" height="24">Data:</td>
    <td width="265"><input type="text" name="data" maxlength="10" OnKeyPress="formatar('##/##/####', this)" ></td>
  </tr>
  <tr>
    <td height="24">Tel.:</td>
    <td><input type="text" name="tel" maxlength="12" OnKeyPress="formatar('##-####-####', this)" ></td>
  </tr>
  <tr>
    <td height="24">Cep:</td>
    <td><input type="text" name="cep" maxlength="9" OnKeyPress="formatar('#####-###', this)" ></td>
  </tr>
  <tr>
    <td height="24">CPF:</td>
    <td><input type="text" name="cpf" maxlength="14" OnKeyPress="formatar('###.###.###-##', this)" ></td>
  </tr>
  <tr>
    <td height="24">Hora:</td>
    <td><input type="text" name="hora" maxlength="5" OnKeyPress="formatar('##:##', this)" ></td>
  </tr>
</table>

1 answer

2


Exchange the onkeypress for onkeydown. Apparently the keypress is not recognized on mobile keyboards.

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);
  }
  
}
.tabela {
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 10px;
}
<table width="301" height="160" border="0" cellspacing="0" class="tabela">
  <tr>
    <td height="34" colspan="2" valign="top">Este script formata qualquer tipo de campo.<br>
Veja o Exemplo abaixo</td>
  </tr>
  <tr>
    <td width="32" height="24">Data:</td>
    <td width="265"><input type="text" name="data" maxlength="10" onkeydown="formatar('##/##/####', this)" ></td>
  </tr>
  <tr>
    <td height="24">Tel.:</td>
    <td><input type="text" name="tel" maxlength="12" onkeydown="formatar('##-####-####', this)" ></td>
  </tr>
  <tr>
    <td height="24">Cep:</td>
    <td><input type="text" name="cep" maxlength="9" onkeydown="formatar('#####-###', this)" ></td>
  </tr>
  <tr>
    <td height="24">CPF:</td>
    <td><input type="text" name="cpf" maxlength="14" onkeydown="formatar('###.###.###-##', this)" ></td>
  </tr>
  <tr>
    <td height="24">Hora:</td>
    <td><input type="text" name="hora" maxlength="5" onkeydown="formatar('##:##', this)" ></td>
  </tr>
</table>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.