How to convert the characters inside the form and print on screen?

Asked

Viewed 202 times

1

How do I make the characters of - are converted into : number inside the form.

form

When pasting a number inside the form, as in the example above; I want the - are converted into :, getting 00:00:00:00.

function substituiPonto(){
  valor = document.calcform.visor.value;
  document.getElementById("visor").value = valor.replace("-", ":");
}
function tela(){
  window.alert(valor);
}
<form name="calcform" method="post" action="">
  <input type="text" name="visor" id="visor" value="" onKeyPress="substituiPonto()"/>
</form>

For that code I can only convert while I type, and not convert when I give CTRL+V within the form.

  • Could include the property onfocusout="substituiPonto()" in his input. In his Javascript, instead of using the function replace(), would use replaceAll(). Thus, by removing the focus of its input, would call the function substituiPonto(), and makes a replace in all the - for :.

  • Could you show me what the code looks like for you?

  • I put in the answer Lucas, see if it solves your problem

  • Excellent friend! Thank you very much!

1 answer

1


Putting the onfocusout="substituiPonto(), taking advantage of its function substituiPonto(), just replacing replace() for replaceAll(). When glue (CTRL+V) the format 00-00-00 for example, and when leaving your input, that is, in the event onfocusout, will be called substituiPonto(), replacing the whole - found by :. Then you could display in the format you want, using :.

function substituiPonto(){
  valor = document.calcform.visor.value;
  document.getElementById("visor").value = valor.replaceAll("-", ":");
}
function tela(){
  window.alert(valor);
}
<form name="calcform" method="post" action="">
  <input type="text" name="visor" id="visor" value="" onKeyPress="substituiPonto()" onfocusout="substituiPonto()"/>
</form>

Browser other questions tagged

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