I need an if that changes the mask of an input

0
Hello, I have a php mysql code, and I need help, the code is ready, I just have the option to edit the data, that's what I need help with, I'll explain what's happening, I have my code has three inputs, the first asks what will be edited the second I asked for the new value and the third one where this value is, all are ready but I would like to know if there is any way to after that I put what will be edited he put a mask on the new value ex: What's going to be edited ?: Zip Code

New value: 89212-530 (note the dash that's what I want, but I want it to only appear if the first data is zipped)
where: customers
NOTE: I'm using a jquery masquerade plugin

<form action="editar.php" method="POST" class="envio_de_dados3 panel panel-default">
				<h4 class="panel-heading ">Cpf da Pesssoa cujo registro será alterado</h4><input type="text" class="form-control input-sm" id="cpf2" name="edit"><br>
				<h4 class="panel-heading ">Oque será editado</h4><input type="text" class="form-control input-sm" id="WE" name="editt"><br>
				<h4 class="panel-heading ">Novo Valor</h4><input type="text" class="form-control input-sm" name="editado"><br><br>
				<input type="submit"><br>
</form>
    
asked by anonymous 28.04.2016 / 16:09

1 answer

1

Using JavaScript , is quite simple and increases the usability of a form.

For example, in a register with several fields, a person will not waste time pausing to enter the numbers to put dashes or also, for the correction of the data since it is much more organized.

Code Description JavaScript e HTML :

 function mascara(t, mask){
 var i = t.value.length;
 var saida = mask.substring(1,0);
 var texto = mask.substring(i)
 if (texto.substring(0,1) != saida){
 t.value += texto.substring(0,1);
 }
 }
<form name="cadatro">
 <table width="500px" align="center">
 <tr>
 <td width="100px">
 <b>CEP:</b>
  </td>
 <td>
 <input type="text" name="cep" onkeypress="mascara(this, '#####-###')" maxlength="9">
 </td>
 </tr>
 <tr>
 <td>
 <b>Telefone:</b>
 </td>
 <td>
 <input type="text" name="telefone" onkeypress="mascara(this, '## ####-####')" maxlength="12">
 </td>
 </tr>
 <tr>
 <td>
 <b>Celular:</b>
 </td>
 <td>
 <input type="text" name="celular" onkeypress="mascara(this, '## #####-####')" maxlength="13">
 </td>
 </tr>
 <tr>
 <td colspan="2">
 <input type="submit" value="Enviar">
 <input type="reset" value="Limpar">
 </td>
 </tr>
 </table>
 </form>

If you want to use also for CPF, RG, etc ... just change the positions and characters of this part onkeypress="mascara(this, '## #####-####')"

For CPF:

 <input type="text" name="cpf" onkeypress="mascara(this, '###.###.###-##')" maxlength="9">
    
28.04.2016 / 16:29