Title, a lot of online search is not very good to achieve. My implementation needs are as follows:
1, if the input number without a decimal point, then automatically add two decimal places, such as: Enter 5, replace with 5.00
2, enter 5., replace with 5.00
3, enter 5.1, replace with 5.10
4, input non-digital, automatic emptying
The JavaScript code is as follows:
//limit input numbers, only two-bitfunction Checknum (obj) {//Check whether a non-numeric value if(IsNaN (Obj.value)) {Obj.value="0.00"; return; } if(obj! =NULL) { //Auto-fill 0 without decimals if(Obj.value.indexOf ('.') == -1) { if(Obj.value = ="") {Obj.value="0.00"; } Else{Obj.value+=". XX"; } } if(Obj.value.toString (). Split (".")[1].length <2) { if(Obj.value.toString (). Split (".")[1].length = =0) {Obj.value+="xx"; } if(Obj.value.toString (). Split (".")[1].length = =1) {Obj.value+="0"; } } //check for two-bit after decimal point if(Obj.value.toString (). Split ("."). length >1&& obj.value.toString (). Split (".")[1].length >2) {Obj.value= Obj.value.substring (0, Obj.value.indexOf ('.') +3); }}}function Checknumforenter (obj) {//Enter event intercept if(Event. keycode = = -) { This. Checknum (obj); }}
Here's how to use it:
<asp:textbox id="txtb_minamount" runat="server" width= " 200px "onblur="checknum (this)" onkeypress="checknumforenter ( This)"></asp:TextBox>
Note that you need to intercept onblur,onkeypress two events.
Input uses JavaScript to restrict entering numbers with decimals