JS can only enter numbers or numbers and letters and other regular expressions _ regular expressions

Source: Internet
Author: User
JS can only be a number and a decimal point
0. Cannot enter Chinese
1) <input onpaste= "return false;" type= "text" name= "TextField" style= "width:400px"; Ime-mode:disabled "Value=" ">
2)
Copy Code code as follows:

<script>
function Chkit (frm) {
if (Frm.n1.value.length>0&&frm.n1.value.match (/[\x01-\xff]*/) ==false) {
Alert (' N1 cannot enter Chinese! ')
Frm.n1.focus ();
return false;
}
}
</script>
<body>
<form onsubmit= "return Chkit" >
<input name= "N1" >
<input name= "N2" >
<input name= "SMT" type= "submit" value= "submitted" >
</form>
</body>

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 name=txt1 onchange= "if (/\d/.test (this.value)) {alert (' can only enter numbers '); this.value= ';} ' >
3. Number and decimal method two
<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*?)? $/)) 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 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 ') >

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 numerals can be entered:
<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>
<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
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, ') "
Do not allow special characters and spaces to be entered:
<input id= "code" onkeypress= "Return Validatespecialcharacter ()," onblur= "Validate (This)"/>

————————————————————————————————————————
Cannot be empty
<input onblur= "if (This.value.replace (/^ +| +$/g,") = = ") alert (' cannot be empty! ')" >
Judging characters consist of letters and numbers, underscores, and dots. And the beginning can only be underlined and letters.
/^ ([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, ")" >
can only enter Chinese
<input type= "text" onkeyup= "Value=value.replace (/[^\u4e00-\u9fa5]/g, ')" >
can only enter English
<input type= "text" onkeyup= "Value=value.replace (/[^\a-\z\a-\z]/g, ')" >
<input type= "text" onkeyup= "Value=value.replace (/[^a-za-z]/g, ')" >
--------------------------------------------------------------------------------------------------------------- -----

You can only enter Chinese, English, numerals, @ 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 and the Paste menu cannot be popped
<input type= "text" onkeyup= "Value=value.replace (/[^\a-\z\a-\z]/g, '") "onkeydown=" Fnckeystop (event) "Onpaste=" return false ' OnContextMenu = ' return false '/>
You can only enter numbers and dots (note: in [^\d\.] D can not be written in uppercase D, otherwise it will become all except the number of characters)
<input name= "Price" type= "text" size= "8" maxlength= "8" onkeyup= "Value=value.replace" (/[^\d\.] /g, ') ' >
All in all: First enter onkeyup= in <input> "Value=value.replace (/[^\x]/g,") and then in (/[\x]/g, "") Replace the X you want to enter the code you can
Chinese: u4e00-u9fa5
Number: D, 0-9
English: A-Z, A-Z
Other symbols @, dots, or other symbols. Can also be multiple, separated on the line.
For example:
Chinese, English and digit @ sign with dot symbol: \a-\z\a-\z0-9\u4e00-\u9fa5\@\.
If you want to be in the text box can not right-click pop-up menu and can not paste into the copy of the information in the <input> input onkeydown= "Fnckeystop (event)" onpaste= "return false" OnContextMenu = "return false;"
--------------------------------------------------------------------------------------------------------------- ------------------------

First, you only allow numbers and decimal points to be entered.
Copy Code code as follows:

<input onkeypress= "return (/[/d.] /.test (String.fromCharCode (Event.keycode)) "style=" ime-mode:disabled ">

Second, judge the more detailed, even 22. 2 It's not a number, it's a judgment.
Copy Code code as follows:

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

Third, only integer input is allowed. In fact, can also be based on the third article to extrapolate to do some restrictions.
Copy Code code as follows:

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

conclusion, in fact
Style= "ime-mode:disabled
This sentence is more practical. Closes the input method. Saves some people to open the full angle input number, the result input does not go in to seek your kutianmalei, also blames you to design is not good.
Only allow numbers to be entered
<input name= "username" type= "text" onkeyup= "Value=this.value.replace (//d+/g, ')" >
Only English letters, numbers, and underscores are allowed (in the following two ways)
<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 &=@ are allowed
<input name= "username" type= "text" onkeyup= "Value=value.replace (/[^/w=@&]|_/ig, ')" >
Only allow 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 &=@ are allowed
<input name= "username" type= "text" onkeyup= "Value=value.replace (/[^/w=@&]|_/ig, ')" >
Only allow 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.