Remove leading zeros from input type=number
Solution 1
just use a regular expression like this
textboxText= textboxText.replace(/^0+/, '')
Solution 2
I'm going to give an example with react usage. It uses state
which store the value of the field. But, I think u can replace it with whatever variable you like.
<input type='number' value={Number(this.state.myNumber).toString()}/>
In my case, myNumber
stores a year number. This way, the year 2018
(for example) won't be displayed mistakenly as 02018
.
Solution 3
Html input tags always return text, not numbers, even its content can be coerced to numerical format, dates, etc...
So next you should convert that input to actual number format:
parseInt(myNum); // If you expect an integer.
parseFloat(myNum); // If you expect floating point number.
Comments
-
Alessio Dal Bianco over 2 years
I noticed that if i use
<input type="number" />
the leading zeros are not removed. I also saw a lot of discussion on how keeping leading zeros.For example "000023" and "23" are the same number and i think that it doesn't make sense keeping those zeros.