Javascript restricts that only integer or decimal point numbers can be entered in the text box. js decimal point

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.