The regular expression limits that only numbers can be entered in the text box.

Source: Internet
Author: User
The regular expression limits that only numbers can be entered in the text box.  

In many cases, we need to restrict the type of text box input content when creating a form. Below we use a regular expression to restrict the text box to enter only numbers, decimal places, English letters, Chinese characters, and so on.Code.

1. Only numeric code can be entered in the text box (decimal point cannot be entered)
<Input onKeyup = "This. value = This. value. Replace (/\ D/g,'') "onAfterpaste = "This. value = This. value. Replace (/\ D/g,'') ">

2. only numbers are allowed, and decimal points are allowed.
keyup = "If (isnan (value )) execcommand ('undo ') "on afterpaste = "If (isnan (value )) execcommand ('undo ') ">
change = "If (/\ D /. test (this. value) {alert ('only numbers allowed '); this. value = '';}">

3. Number and decimal point method 2
<Input type = text 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} ">

Can be encapsulated into separate functions

Function keypress (OB ){
If (! Ob. value. Match (/^ [\ + \-]? \ D *? \.? \ D *? $/) OB. value = OB. t_value; else OB. t_value = OB. value; If (OB. value. Match (/^ (? : [\ + \-]? \ D + (? : \. \ D + )?)? $/) OB. o_value = OB. value;
}
Function keyup (OB ){
If (! Ob. value. Match (/^ [\ + \-]? \ D *? \.? \ D *? $/) OB. value = OB. t_value; else OB. t_value = OB. value; If (OB. value. Match (/^ (? : [\ + \-]? \ D + (? : \. \ D + )?)? $/) OB. o_value = OB. value;
}
Function onblur (OB ){
If (! Ob. value. Match (/^ (? : [\ + \-]? \ D + (? : \. \ D + )? | \. \ D *?)? $/) Ob. value = ob. o_value; else {If (OB. value. match (/^ \. \ D + $/) ob. value = 0 + OB. value; If (OB. value. match (/^ \. $/) ob. value = 0; ob. o_value = ob. value };
}

You only need to input this object in the call!

4. Only letters and Chinese characters can be entered
<Input onKeyup = "value = value. Replace (/[\ D]/g,'') "onBeforepaste = "clipboardData. setdata ('text', clipboardData. getdata ('text '). replace (/[\ D]/g, '')" maxlength = 10 name = "Numbers">

5. Only English letters and numbers are allowed. Chinese characters are not allowed.
<Input onKeyup = "value = value. Replace (/[^ \ W \. \/]/ig,'') ">

6. Only numbers and English letters can be entered.
<Input onKeyup = "value = value. Replace (/[^ \ d | Chun]/g,'') ">

7. After the decimal point, you can enter a maximum of two digits (numbers and Chinese characters). You cannot enter letters or operator numbers:
<Input onKeypress = "If (event. keycode <48 | event. keycode> 57) & event. keycode! = 46 |/\. \ D $/. Test (value) event. returnvalue = false ">

8. A maximum of two digits (numbers, letters, and Chinese characters) can be entered after the decimal point. You can enter the operator number:
<Input onKeyup = "this. value = This. value. replace (/^ (\-) * (\ D + )\. (\ D ). * $/, '$1 $2. $ 3') ">

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.