JS restricted text box can only enter the number of characters letters and prohibit the space code

Source: Internet
Author: User

1. Text box can only enter a numeric code (decimal point can not enter)

The code is as follows Copy Code
<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.

The code is as follows Copy Code
<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

The code is as follows Copy Code
<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 (/^ (?: [+-]?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 a separate function

The code is as follows Copy Code

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};
}

Just call and pass in the This object!

4. Only letters and characters can be entered

The code is as follows Copy Code
<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

The code is as follows Copy Code
<input onkeyup= "Value=value.replace (/[^w./]/ig, ')" >

6. Only numbers and English can be entered

The code is as follows Copy Code
<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:

The code is as follows Copy Code
<input onkeypress= "If" (event.keycode<48 | | event.keycode>57) && event.keycode!=46 | |/.dd$/.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:

The code is as follows Copy Code
<input onkeyup= "This.value=this.value.replace (/^ (-) * (d+). ( DD). *$/, ' $1$2.$3 ' >


Only Chinese characters can be entered:

The code is as follows Copy Code

<input onkeyup= "Value=value.replace (/[^u4e00-u9fa5]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^u4e00-u9fa5]/g, ') ">

Only numbers can be entered:

The code is as follows Copy Code

<input onkeyup= "Value=value.replace (/[^d]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^d]/g, ') ">


Only English and numerals can be entered:

The code is as follows Copy Code
<input onkeyup= "Value=value.replace (/[w]/g,") "Onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^d]/g, ') ">

The control input box can only enter text or numbers, or it may not allow special characters to be entered
It is not allowed to enter the following characters: (like!@#$%^&*, etc.) <br>

The code is as follows Copy Code

<textarea rows=2 cols=20 name=comments onkeypress= "if (Event.keycode > && event.keycode < 48) | | (Event.keycode > && event.keycode < 65) | | (Event.keycode > && event.keycode < 97)) Event.returnvalue = false; " >

No space input only

The code is as follows Copy Code
Onkeyup= "Value=value.replace (/s/g,") "

Only Chinese and English can be entered:

  code is as follows copy code
onkeyup= " Value=value.replace (/[^a-za-zu4e00-u9fa5]/g, ') "onbeforepaste=" Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^a-za-zu4e00-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.