Restricting text boxes with regular expressions only numbers, decimal points, English letters, Chinese characters, etc. regular expressions

Source: Internet
Author: User

1. Text box can only enter a numeric code (decimal point can not enter)
<input onkeyup= "This.value=this.value.replace (/\d/g,") "Onafterpaste=" This.value=this.value.replace (/\D/g, " ) ">

Or

<input onkeyup= ' if (this.value.length==1) {this.value=this.value.replace (/[^1-9]/g, ')}else{this.value= This.value.replace (/\d/g, ')} ' onafterpaste= ' if (this.value.length==1) {this.value=this.value.replace (/[^1-9]/g, ' ')}else{this.value=this.value.replace (/\d/g, ')} ' >

2. Only numbers can be entered and the decimal point may be lost.

<input type= "text" size= "onkeyup=" This.value=this.value.replace (/[^\d\.] +?/g, ")"/>
<input onkeyup= "if (isNaN (value)) ExecCommand (' Undo ') ' onafterpaste= ' if (isNaN (value)) ExecCommand (' Undo ') >
<input name=txt1 onchange= "if (/\d/.test (this.value)) {alert (' can only enter numbers '); this.value= ';} ' >

3. Number and decimal method two
<input type=text tvalue= "" ovalue= "" onkeypress= "if!this.value.match (/^[\+\-]?\d*?\.? \d*?$/)) This.value=this.t_value;else this.tvalue=this.value;if (This.value.match/^ (?: [\+\-]?\d+ (?: \. \d+)? $/)) This.ovalue=this.value "onkeyup=" if (!this.value.match/^[\+\-]?\d*?\.? \d*?$/)) This.value=this.t_value;else this.tvalue=this.value;if (This.value.match/^ (?: [\+\-]?\d+ (?: \. \d+)? $/)) This.ovalue=this.value "onblur=" if (!this.value.match (?: [/^ (?: \. \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.ovalue=this.value} ">

4. Only letters and 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. Can only enter English letters and numbers, can not enter Chinese
<input onkeyup= "Value=value.replace (/[^\w\.\/]/ig, ')" >

6. Only digital and English <font color= "Red" >chun</font>
<input onkeyup= "Value=value.replace (/[^\d|chun]/g, ')" >

7. Only a maximum of two digits after the decimal point (number, Chinese can be entered), can not enter letters and operational symbols:
<input onkeypress= "If" (event.keycode<48 | | event.keycode>57) && event.keycode!=46 | |/\.\d\d$/.test ( Value)) Event.returnvalue=false ">

8. After the decimal point can only have up to two digits (numbers, letters, Chinese can be entered), you can enter operational symbols:
<input onkeyup= "This.value=this.value.replace (/^ (\-) * (\d+) \. ( \d\d). *$/, ' $1$2.$3 ') >






JavaScript is a common form entry restriction feature that restricts user input when entering:

1. Text box can only enter a numeric code (decimal point can not enter)
< input onkeyup= "This.value=this.value.replace (/\d/g,") "Onafterpaste=" This.value=this.value.replace (/\D/g, " ) ">

2. Only numbers can be entered and the decimal point may be lost.
< input onkeyup= ' if (isNaN (value)) ExecCommand (' Undo ') ' onafterpaste= ' if (isNaN (value)) ExecCommand (' Undo ') >
< input onchange= "if (/\d/.test (this.value)) {alert (' only digits '); this.value= ';} ' name=txt1>

3. Number and decimal method two
< input Type=text tvalue= "" ovalue= "" onkeypress= "if!this.value.match (/^[\+\-]?\d*?\.? \d*?$/)) This.value=this.t_value;else this.tvalue=this.value;if (This.value.match/^ (?: [\+\-]?\d+ (?: \. \d+)? $/)) This.ovalue=this.value "onkeyup=" if (!this.value.match/^[\+\-]?\d*?\.? \d*?$/)) This.value=this.t_value;else this.tvalue=this.value;if (This.value.match/^ (?: [\+\-]?\d+ (?: \. \d+)? $/)) This.ovalue=this.value "onblur=" if (!this.value.match (?: [/^ (?: \. \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.ovalue=this.value} ">

4. Only letters and 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. Can only enter English letters and numbers, can not enter Chinese
< input onkeyup= "value=value.replace (/[^\w\.\/]/ig, ')" >

6. Only numbers and English can be entered
< input onkeyup= "value=value.replace (/[^\d|chun]/g, ')" >

7. Only a maximum of two digits after the decimal point (number, Chinese can be entered), can not enter letters and operation symbols
< input onkeypress= "if (event.keycode<48 | | event.keycode>57) && event.keycode!=46 | |/\.\d\d$/.test ( Value)) Event.returnvalue=false ">

8. Only a maximum of two digits after the decimal point (digits, letters, Chinese can be entered), you can enter operational symbols
< input onkeyup= "This.value=this.value.replace (/^ (\-) * (\d+) \. ( \d\d). *$/, ' $1$2.$3 ') >

9. Only Chinese can be entered:

< input name= "realname" type= "text" maxlength= "class=" Input_style "value=" "onkeyup=" Value=value.replace (/[^\ u4e00-\u9fa5]/g, "onbeforepaste=" clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^\ U4e00-\u9fa5]/g, ")"/>

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.