JS can only enter regular expressions such as numbers, numbers, and letters.

Source: Internet
Author: User

Note: This article excerpt to: http://www.cnblogs.com/yzenet/archive/2012/04/10/2440312.html

JS can only be judged by numbers and decimal points.
0. Cannot enter Chinese
1) <input onpaste= "return false;" type= "text" name= "TextField" style= "width:400px; ime-mode:disabled "Value=" >
2) <script>
function Chkit (frm) {
if (Frm.n1.value.length>0&&frm.n1.value.match (/[\x01-\xff]*/) ==false) {
Alert (' N1 can't input Chinese! ‘)
Frm.n1.focus ();
return false;
}
}
</script>
<body>
<form onsubmit= "return Chkit (This)" >
<input name= "N1" >
<input name= "N2" >
<input name= "SMT" type= "submit" value= "Submission" >
</form>
</body>

1. Text box can only enter a numeric code (the decimal point can not be entered)
<input onkeyup= "This.value=this.value.replace (/\d/g, ')" onafterpaste= "This.value=this.value.replace (/\D/g, ' ) ">
2. Only the number can be entered and the decimal point will be lost.
<input onkeyup= "if (IsNaN (value)) ExecCommand (' Undo ')" Onafterpaste= "if (IsNaN (value)) ExecCommand (' Undo ')" >
<input name=txt1 onchange= "if (/\d/.test (this.value)) {alert (' Input number only '); this.value= ';}" >
3. Number and decimal method two
<input type=text t_value= "" o_value= "" onkeypress= "if (!this.value.match.? \d*?$/)) This.value=this.t_value;else this.t_value=this.value;if (This.value.match (?: [/^ (?: \. \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+)?)? $/)) This.o_value=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.o_value=this.value} ">
4. Only letters and kanji 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 can be entered, not Chinese
<input onkeyup= "Value=value.replace (/[^\w\.\/]/ig, ')" >
6. Only enter numbers 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 (numeric, 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 (numbers, letters, Chinese can be entered), you can enter the operation symbol:

<input onkeyup= "This.value=this.value.replace (/^ (\-) * (\d+) \. ( \d\d). *$/, ' $1$2.$3 ') ">

Prohibit special characters:

Onkeypress= "if (Event.keycode < | | Event.keycode >) event.returnvalue = false;"

Only Chinese characters can be entered:
<input onkeyup= "Value=value.replace (/[^/u4e00-/u9fa5]/g, ')" onbeforepaste= "Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^/u4e00-/u9fa5]/g, ')) ">

style= "ime-mode:disabled" forbidden Chinese Character input method

Only numbers can be entered:

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


Only English and numbers can be entered:
<input onkeyup= "Value=value.replace (/[/w]/g, ')" onbeforepaste= "Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^/d]/g, ')) ">

Control input Box can only enter text or numbers, or you can not allow special characters to be entered
You are not allowed to enter the following characters: (like ^&*, etc.) <br>
<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; " >

Only whitespace input is forbidden
Onkeyup= "Value=value.replace (//s/g, ')"

Onkeydown= "if (event.keycode==32) return false"

Only Chinese and English can be entered:
Onkeyup= "Value=value.replace (/[^/a-za-z/u4e00-/u9fa5]/g, ')" onbeforepaste= "Clipboarddata.setdata (' text ', Clipboarddata.getdata (' text '). Replace (/[^/a-za-z/u4e00-/u9fa5]/g, ')) "

You are not allowed to enter special characters and spaces:

<input id= "code" onkeypress= "Return Validatespecialcharacter ();" Onblur= "Validate (This)"/>

————————————————————————————————————————

cannot be empty
<input onblur= "if (This.value.replace (/^ +| +$/g, ') = =") alert (' cannot be empty! ') " >

The judging character consists of a letter and a number, an underscore, and a dot. And only the underscore and the letter can begin
/^ ([A-za-z_]{1}) ([\w]*) $/g.test (str)

Only numbers can be entered
<input name= "text" type= "text" id= "NewPage" onkeyup= "Value=value.replace (/\d/g, ')" onafterpaste= "value= Value.replace (/\d/g, ') ">

You can only enter Chinese
<input type= "text" onkeyup= "Value=value.replace (/[^\u4e00-\u9fa5]/g, ')" >

Can only be entered in English
<input type= "text" onkeyup= "Value=value.replace (/[^\a-\z\a-\z]/g, ')" >
<input type= "text" onkeyup= "Value=value.replace (/[^a-za-z]/g, ')" >

--------------------------------------------------------------------------------------------------------------- -----

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

2. Only the number can be entered and the decimal point will be lost.
<input onkeyup= "if (IsNaN (value)) ExecCommand (' Undo ')" Onafterpaste= "if (IsNaN (value)) ExecCommand (' Undo ')" >
<input name=txt1 onchange= "if (//d/.test (this.value)) {alert (' Input number only '); this.value= ';}" >

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

4. Only letters and kanji 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 can be entered, not Chinese
<input onkeyup= "Value=value.replace (/[^/w/.//]/ig, ')" >

6. Only enter numbers 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 (numeric, 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 (numbers, letters, Chinese can be entered), you can enter the operation symbol:
<input onkeyup= "This.value=this.value.replace (/^ (/-) * (/d+)/. ( /D/D). *$/, ' $1$2.$3 ') ">

You can only enter Chinese, English, numbers, @ symbols, and. Symbols
<input type= "text" onkeyup= "Value=value.replace (/[^\a-\z\a-\z0-9\u4e00-\u9fa5\@\.] /g, ') ' >

Only English is allowed and cannot be pasted or pop-up menu
<input type= "text" onkeyup= "Value=value.replace (/[^\a-\z\a-\z]/g, ')" onkeydown= "Fnckeystop (event)" Onpaste= " return false "OnContextMenu =" return false "/>

Only numbers and dot numbers can be entered ( Note : in [^\d\.] D cannot be written in uppercase D, otherwise it becomes all characters except the number.
<input name= "Price" type= "text" size= "8" maxlength= "8" onkeyup= "Value=value.replace (/[^\d\.] /g, ') ' >

All in all: First enter onkeyup= "Value=value.replace (/[^\x]/g,") in <input> and then X in (/[\x]/g, ") to the code you want to enter.

English : u4e00-u9fa5
numbers : D, 0-9
English : A-Z, A-Z
Other symbols @, dots, or other symbols. Can also be multiple, with \ Separated on the line.
For example:
Add symbols in Chinese, English, and digital plus @ symbols: \a-\z\a-\z0-9\u4e00-\u9fa5\@\.

If you want to not right-click in the text box pop-up menu and can not paste into the copied information, you will enter the <input> onkeydown= "Fnckeystop (event)" onpaste= "return false" OnContextMenu = "return false;"

--------------------------------------------------------------------------------------------------------------- ------------------------

one is to allow only numbers and decimal points to be entered.
<input onkeypress= "return (/[/d.) /.test (String.fromCharCode (Event.keycode))) "style=" ime-mode:disabled ">

Second , the judgment is more detailed, even 22. 2 That's not a number.

<script>
function Check () {
if (IsNaN (tt.value))
{alert ("illegal character! ");
tt.value= "";}
}
</script>
<input type= "text" Name= "tt" onkeyup= "check ();" >

the third, only allows the input of integers. In fact, we can also make some restrictions according to the third article extrapolate.

<script language=javascript>
function Onlynum ()
{
if (! ( EVENT.KEYCODE==46) &&! (event.keycode==8) &&! (event.keycode==37) &&! (event.keycode==39))
if (! ( (event.keycode>=48&&event.keycode<=57) | | (event.keycode>=96&&event.keycode<=105)))
Event.returnvalue=false;
}
</script><input onkeydown= "Onlynum ();" Style= "ime-mode:disabled>
the conclusion, in fact

style= "Ime-mode:disabled
This sentence is more practical. Turn off the IME. Save some people to open the full-width input number, the result input does not come in to find you kutianmalei, also blame you design bad.

allow only numeric input
<input name= "username" type= "text" onkeyup= "Value=this.value.replace (//d+/g, ')" >

only English letters, numbers, and underscores are allowed (the following two methods are implemented)
<input name= "Userna first, only numbers and decimal points are allowed.
<input onkeypress= "return (/[/d.) /.test (String.fromCharCode (Event.keycode))) "style=" ime-mode:disabled ">

Second , the judgment is more detailed, even 22. 2 That's not a number.

<script>
function Check () {
if (IsNaN (tt.value))
{alert ("illegal character! ");
tt.value= "";}
}
</script>
<input type= "text" Name= "tt" onkeyup= "check ();" >

the third, only allows the input of integers. In fact, we can also make some restrictions according to the third article extrapolate.

<script language=javascript>
function Onlynum ()
{
if (! ( EVENT.KEYCODE==46) &&! (event.keycode==8) &&! (event.keycode==37) &&! (event.keycode==39))
if (! ( (event.keycode>=48&&event.keycode<=57) | | (event.keycode>=96&&event.keycode<=105)))
Event.returnvalue=false;
}
</script><input onkeydown= "Onlynum ();" Style= "ime-mode:disabled>
the conclusion, in fact

style= "Ime-mode:disabled
This sentence is more practical. Turn off the IME. Save some people to open the full-width input number, the result input does not come in to find you kutianmalei, also blame you design bad.

allow only numeric input
<input name= "username" type= "text" onkeyup= "Value=this.value.replace (//d+/g, ')" >

only English letters, numbers, and underscores are allowed (the following two methods are implemented)
<input name= "username" type= "text" style= "ime-mode:disabled" >
<input name= "username" type= "text" onkeyup= "Value=value.replace (/[^/w/.//]/ig, ')" >

only English letters, numbers and &[email are allowed protected]
<input name= "username" type= "text" onkeyup= "Value=value.replace (/[^/[email protected]&]|_/ig, ')" >

allow only Chinese characters to be entered
<input name= "username" type= "text" onkeyup= "Value=value.replace (/[^/u4e00-/u9fa5]/g, ')" >
Me "type=" text "style=" ime-mode:disabled ">
<input name= "username" type= "text" onkeyup= "Value=value.replace (/[^/w/.//]/ig, ')" >

only English letters, numbers and &[email are allowed protected]
<input name= "username" type= "text" onkeyup= "Value=value.replace (/[^/[email protected]&]|_/ig, ')" >

allow only Chinese characters to be entered
<input name= "username" type= "text" onkeyup= "Value=value.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.