How to realize the function of entering only two decimal places in javascript
This article introduces you how javascript can only input two decimal places, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Method: 1. Add "oninput=" value=value.toString (). Match (/ ^\ d + (?:\. D {0Magne2})? /) to the input tag.
2. Bind the onimput event to the input tag, which is realized by using rules in the handling function.
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
Javascript limit input box can only enter numbers with two decimal places
Method 1:
You can only enter two decimals.
Method 2: bind the onimput event to the input tag, and in the event handler, use regular expressions to
Implementation code 1:
Var vv = ""; document.getElementById ("put"). Oninput=function () {var val = this.value.replace (/\. /, "); var valArr=this.value.split ('.'); if ((/\ Dhand g) .test (val) | | valArr.length > 2 | | valArr.length > 1&&Number (valArr [1]) > 99) {this.value=vv;}}
Implementation code 2:
Var vv = ""; document.getElementById ("put"). Oninput=function () {if (! (/ ^\ d + (.\ d {0Magne2})? $/) .test (this.value)) {this.value=vv;} vv.this.value; return false;}
On how to achieve javascript can only enter two decimal places to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.