I need an if that changes the mask of an input

Asked

Viewed 110 times

0

Hello, I have a mysql php code, and I need help, the code is already ready just lack the option to edit the data, this is what I need help with, I will explain what is happening, I have my code it has three inputs, the first asks what will be edited the second asked the new value and the third where is this value, all are ready but I wonder if there is any way after I put what will be edited he put a mask on the new value ex: what will be edited?: ZIP code

New value: 89212-530 (note the dash is what I want, but I want it to appear only if the first die is cep)
where: customers
NOTE: I am using a jquery plugin for masks (jquery maskedinput)

<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>

  • If the answer helped, mark as solution ;)

  • helped a little but what I wanted is that when you sent to the server that the value you want to edit is the cep it would put a mask on the new value of the variable cep, but only if the value that will be edited is the cep if it is not I do not want the mask to appear. do you understand? kk

  • I understand, apply only in right zip code?

  • It is not easier to add a new field with the name ZIP ? Then the person will only type the ZIP code

  • 1

    i’ll do it another way, thanks for the help Victor I’ll mark your answer as sure after you were the one who tried to help, thanks for that :D

1 answer

1


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

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

Manure from the Code 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 that part onkeypress="mascara(this, '## #####-####')"

For CPF:

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

Browser other questions tagged

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