Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 319
Alessio Dal Bianco Avatar asked Jun 03 '15 16:06

Alessio Dal Bianco


People also ask

How do you remove leading zeros from input?

If it is set to type "text", there are no issues, leading zeros are removed as expected. In both cases, a console. log() shows that the value stored in state is indeed stripped of leading zeros (a single 0 is allowed as a value.)

How do I remove leading zeros in typescript?

To remove the leading zeros from a number, call the parseInt() function, passing it the number and 10 as parameters, e.g. parseInt(num, 10) . The parseInt function parses a string argument and returns a number with the leading zeros removed.

How do I remove zeros in front of a number in Python?

Use the lstrip() Function Along With List Comprehension to Remove Leading Zeros in a String in Python. The lstrip() can be utilized to remove the leading characters of the string if they exist. By default, a space is the leading character to remove in the string.

How do you prevent a zero as the first character when typing in input type number?

keypress(function(evt) { if (evt. which == "0".


1 Answers

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.

like image 121
Moses Aprico Avatar answered Sep 22 '22 20:09

Moses Aprico