I noticed that if i use the leading zeros are not removed. I also saw a lot of discussion on how keeping leading ze
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
.
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>
You can try this.
str1 = str.replace(/^0+/,'');
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.
add step="any" to your input tag. if this does not work on your browser, change type to type="tel"
just use a regular expression like this
textboxText= textboxText.replace(/^0+/, '')