Javascript – Remove leading zeros from input type=number


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.

Best Solution

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.