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.
just use a regular expression like this
textboxText= textboxText.replace(/^0+/, '')
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.
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.
HTML5 has <input type="tel"> for this purpose
You can try this.
str1 = str.replace(/^0+/,'');
Try this :
<!DOCTYPE html>
<html>
<body>
<input type="number" id="txtfield" /><button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var myNumber = document.getElementById("txtfield").value;
document.write(parseInt(myNumber ,10));
}
</script>
</body>
</html>
add step="any" to your input tag. if this does not work on your browser, change type to type="tel"
来源:https://stackoverflow.com/questions/30626094/remove-leading-zeros-from-input-type-number