Home > OS >  Can you add number padding to a bootstrap number input box?
Can you add number padding to a bootstrap number input box?

Time:11-06

If I'm using an input box, like so:

<input type="number" value="00001" />

How can I make it so that when pressing the arrow up, it changes to 00002 instead of 2?

enter image description here

Thanks!

CodePudding user response:

"00001" or "00002" are not numbers, but if you still wanna do it, think something like:

Function to add leading zeros at left (num is value, size is the size of the string)

function padLeadingZeros(num, size) {
    var s = num "";
    while (s.length < size) s = "0"   s;
    return s;
}

Then:

$(function() {
      $("input").change(function(e){ // If the value changes
            e.preventDefault();
            $(this).val(padLeadingZeros($(this).val(), 5)); //replace the value with the returned value of the function, 5 is size.
      });
});
  • Related