Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
160
rated 0 times [  167] [ 7]  / answers: 1 / hits: 24894  / 9 Years ago, tue, october 6, 2015, 12:00:00

How can I make an input tag, that always accepts two digits? like 01, 02, 03 etc up to 24. A leading zero should be present in case of single digits(0 to 9)



<input id=hourInput type=number min=1 max=24 step=1  />

More From » html

 Answers
6

Unfortunately it is not possible, in pure HTML5, to achieve this. Javascript will be required...



<input id=hourInput type=number min=1 max=24 step=1 onchange=if(parseInt(this.value,10)<10)this.value='0'+this.value; />


EDIT:
Since this answer seems to get good trafic, I'd like to add the fact that the approach I have suggested is a naïve way to do it and will only correctly work with a min attribute higher than -9. If the number goes lower, the 0 will still get added resulting in 0-234 when the user enter a negative value of 234.


[#64826] Friday, October 2, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lailab

Total Points: 706
Total Questions: 102
Total Answers: 95

Location: Falkland Islands
Member since Mon, Jul 13, 2020
4 Years ago
;