0

Guys i need to fill out an input with zero, to always have 6 characters.
Exemple:
User enters the number 23, while he type, the others 4 characters are 0:

    000000 (start)
000002 (type 2)
000023 (type 3)


I do some searches, but not found and i have no ideia how to do this ;/

  • Hey, here you have a good one. Much better than the answer. https://stackoverflow.com/questions/1267283/how-can-i-pad-a-value-with-leading-zeros – Nahue Gonzalez Sep 11 '20 at 19:00

2 Answers2

0

Try like this:

function padSix(x) 
{
  if (x <= 999999) 
  { 
     x = ("00000"+ x).slice(-6); 
  }
  return x;
}

JSFIDDLE DEMO

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

One solution possible :

var el = document.getElementById('el');
var updatetext = function(){
  el.value = ('000000' + el.value).slice(-6);
}
  
el.addEventListener("keyup", updatetext , false); 
<input id='el' type = 'text' value='000000'>
Anonymous0day
  • 3,012
  • 1
  • 14
  • 16