Remove leading zeros from input type=number

吃可爱长大的小学妹 提交于 2019-12-03 05:45:34

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"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!