Javascript restricts that only integer or decimal point numbers can be entered in the text box. js decimal point
During form verification, is it true that only numbers can be entered in a certain input box? Only integer numbers or numbers with decimal points can be entered. The following code may help you! By defining the onkeypress, onkeyup, and onblur events in the current input box, the results are still very reliable.
Copy codeThe Code is as follows:
<Input type = "text" value = "" t_value = "" o_value = "" onkeypress = "if (! This. value. match (/^ [\ + \-]? \ D *? \.? \ D *? $/) This. value = this. t_value; else this. t_value = this. value; if (this. value. match (/^ (? : [\ + \-]? \ D + (? : \. \ D + )?)? $/) This. o_value = this. value "onkeyup =" if (! This. value. match (/^ [\ + \-]? \ D *? \.? \ D *? $/) This. value = this. t_value; else this. t_value = this. value; if (this. value. match (/^ (? : [\ + \-]? \ D + (? : \. \ D + )?)? $/) This. o_value = this. value "onblur =" if (! This. value. match (/^ (? : [\ + \-]? \ D + (? : \. \ D + )? | \. \ D *?)? $/) This. value = this. o_value; else {if (this. value. match (/^ \. \ d + $/) this. value = 0 + this. value; if (this. value. match (/^ \. $/) this. value = 0; this. o_value = this. value} "/>
The following is a brief explanation of the onkeypress, onkeyup, and onblur events.
The onkeypress event occurs when the keyboard key is pressed and a key is released.
The onkeyup event occurs when the keyboard button is released.
The onblur event occurs when the object loses focus.
In addition, if you need to strictly verify the form, you must perform dual verification on the client and the server. The above is tightly verified on the client. If you have a little understanding of the program, you can easily bypass this verification code. If the server side does not perform verification, it is very easy to have problems.
The above is all the content of this article. I hope you will like it.