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

Source: Internet
Author: User

JS judgment can only be a number or a decimal point
0. Chinese characters cannot be entered
1) <input onpaste = "return false;" type = "text" name = "textfield" style = "width: 400px; ime-mode: disabled" value = "">
2)
Copy codeThe Code is as follows:
<Script>
Function chkIt (frm ){
If (frm. n1.value. length> 0 & frm. n1.value. match (/[\ x01-\ xFF] */) = false ){
Alert ('n1 cannot 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 = "submit">
</Form>
</Body>

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 can be entered, and decimal points can be entered.
<Input onkeyup = "if (isNaN (value) execCommand ('undo ')" onafterpaste = "if (isNaN (value) execCommand ('undo')">
<Input name = txt1 onchange = "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} ">
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. <font color = "Red"> chun </font>
<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') ">

Special characters are prohibited:
OnKeyPress = "if (event. keyCode <45 | event. keyCode> 57) 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" prohibiting Chinese Character Input Methods
Only numbers can be entered:
<Input onkeyup = "value = value. replace (/[^/d]/g, '')" onbeforepaste = "clipboardData. setData ('text', clipboardData. getData ('text '). replace (/[^/d]/g, '')">
Only English letters and numbers 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 contain text, numbers, or special characters.
The following characters cannot be entered: (such as ^ & *) <br>
<Textarea rows = 2 cols = 20 name = comments onKeypress = "if (event. keyCode> 32 & event. keyCode <48) | (event. keyCode> 57 & event. keyCode <65) | (event. keyCode> 90 & event. keyCode <97) event. returnValue = false; ">
Enter spaces only
Onkeyup = "value = value. replace (// s/g ,'')"
Onkeydown = "if (event. keyCode = 32) return false"
Only Chinese and English characters 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 ,''))"
Special characters and spaces are not allowed:
<Input id = "code" onkeypress = "return ValidateSpecialCharacter ();" onblur = "validate (this)"/>

----------------------------------------
Cannot be blank
<Input onblur = "if (this. value. replace (/^ + | + $/g,'') = '') alert ('cannot be blank! ') ">
A judgment character consists of letters, numbers, underscores, and periods. It must start with an underscore or letter.
/^ ([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, '')">
Only Chinese characters can be entered
<Input type = "text" onkeyup = "value = value. replace (/[^ \ u4E00-\ u9FA5]/g,'') ">
Only English can be entered
<Input type = "text" onkeyup = "value = value. replace (/[^ \ a-\ z \ A-\ Z]/g,'') ">
<Input type = "text" onkeyup = "value = value. replace (/[^ a-zA-Z]/g,'') ">
Bytes --------------------------------------------------------------------------------------------------------------------

Only Chinese characters, English letters, numbers, @ symbols, and. symbols can be entered.
<Input type = "text" onkeyup = "value = value. replace (/[^ \ a-\ z \ A-\ Z0-9 \ u4E00-\ u9FA5 \ @ \.] /g, '')">
You can only enter English letters, and the paste menu cannot be displayed.
<Input type = "text" onkeyup = "value = value. replace (/[^ \ a-\ z \ A-\ Z]/g, '')" onkeydown = "fncKeyStop (event) "onpaste =" return false "oncontextmenu =" return false "/>
Only numbers and periods can be entered (Note: d in [^ \ d \.] cannot be written as uppercase D, otherwise it will become all characters except numbers)
<Input name = "price" type = "text" size = "8" maxlength = "8" onkeyup = "value = value. replace (/[^ \ d \.] /g, '')">
All in all: Enter onkeyup = "value = value in <input>. replace (/[^ \ X]/g, '')" and then in (/[\ X]/g ,'') replace X in with the code you want to enter.
English: u4E00-u9FA5
Number: d, 0-9
English: a-z, A-Z
Other symbols @, point, or other symbols. You can also use multiple symbols and separate them.
For example:
English and numbers plus the @ symbol plus some symbols: \ a-\ z \ A-\ Z0-9 \ u4E00-\ u9FA5 \@\.
If you want to right-click the menu in the text box and paste the information that cannot be copied, enter onKeyDown = "fncKeyStop (event) in <input>) "onpaste =" return false "oncontextmenu =" return false ;"
Certificate ---------------------------------------------------------------------------------------------------------------------------------------

First, only numbers and decimal points are allowed.
Copy codeThe Code is as follows:
<Input onKeypress = "return (/[/d.]/. test (String. fromCharCode (event. keyCode)" style = "ime-mode: Disabled">

Second, the judgment is more detailed, and even 22 .. 2 is not counted as a number.
Copy codeThe Code is as follows:
<Script>
Function check (){
If (isNaN (tt. value ))
{Alert ("invalid character! ");
Tt. value = "";}
}
</Script>
<Input type = "text" name = "tt" onkeyup = "check ();">

Third, only integers are allowed. In fact, some restrictions can be made based on article 3.
Copy codeThe Code is 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. To disable the input method. It saves some people the trouble of entering numbers in full angles, and the result cannot be entered to find you crying. It is also a blame for your poor design.
Only numbers are allowed.
<Input name = "username" type = "text" onkeyup = "value = this. value. replace (// D +/g,'') ">
Only English letters, numbers, and underscores (_) are allowed)
<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 @ can be entered @
<Input name = "username" type = "text" onkeyup = "value = value. replace (/[^/w = @ &] | _/ig,'') ">
Only Chinese characters are allowed.
<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 @ can be entered @
<Input name = "username" type = "text" onkeyup = "value = value. replace (/[^/w = @ &] | _/ig,'') ">
Only Chinese characters are allowed.
<Input name = "username" type = "text" onkeyup = "value = value. replace (/[^/u4E00-/u9FA5]/g,'') ">

Related Article

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.