Click here to Skip to main content
15,896,502 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to autofill 0(zero) at the back after the decimal point in jquery? Like if user insert not enough digit then it will auto fill with zero(0) at the back.
Example:
User Insert - 0.12345
Then it must add to - 0.12345000


What I have tried:

This is what I try but I just know how to set the dot symbol can insert only once in the text field.

$("#Lat").on("keypress keyup blur",function (event) {
    $(this).val($(this).val().replace(/[^0-9\.]/g,''));
           if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
               event.preventDefault();
           }
       });

$("#Lng").on("keypress keyup blur",function (event) {
    $(this).val($(this).val().replace(/[^0-9\.]/g,''));
           if ((event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57)) {
               event.preventDefault();
           }
       });
Posted
Updated 28-Aug-18 23:05pm
v2

1 solution

JavaScript
var val = $(this).val(); // the decimal entered
var desired_length = 12; // the length it should 'pad' to - that's the correct term

for (var i = val.length; i < desired_length; i++)
{
    val += '0';
}

Be aware that if you then set the textbox's value to val, using $(this).val(val); then it will alter the caret position.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900