How to directly type the decimals after the user type comma with Javascript?

Asked

Viewed 217 times

-1

I am developing a web site using javascript/jquery and in some fields the user type numeric values. Some of these fields, by business needs, have 9 decimal places:

0,00000000

My problem is that in a case like this, to type, for example, 50.00000, the user needs to type 5 and then several zeros until the 5 is in its correct location.

My question: how do I make the user, by pressing a comma, can already type the decimal places directly, without having to fill in several zeros to form the number he wants?

1 answer

1


Regardless of how you are mounting your mask, a simple option is to initialize the field value:

$("#js").on("keyup", function(ev){
  if (ev.keyCode == 110 || ev.keyCode == 188){
    var valor = $("#js").val();
    $("#js").val( valor + "00000000");
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Simples: <input value="00,00000000" />
<br><br>
Com JS: <input id="js" />

Browser other questions tagged

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