How to enter decimal places directly after user type comma with Javascript?

-1

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

  

0.00000000

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

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

    
asked by anonymous 16.12.2016 / 11:37

1 answer

1

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

$("#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:<inputvalue="00,00000000" />
<br><br>
Com JS: <input id="js" />
    
16.12.2016 / 12:31