Javascript text box verification code based on regular expressions

Source: Internet
Author: User

1, cannot be blank
<Input type = "text" onblur = "if (this. value. replace (/^ + | + $/g, '') ='') alert ('cannot be blank! ') ">

2. Only English and numbers can be entered.
<Input onblur = "if (/[^ 0-9a-zA-Z]/g. test (value) alert ('error')">
<Input onkeyup = "value = value. replace (/[^ 0-9a-zA-Z]/g,'') "/>
<Input type = "text" onkeyup = "value = value. replace (/[^ \ a-\ z \ A-\ Z0-9]/g,'') ">

3. The 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)

4. Only numbers can be entered.
<Input name = "text" type = "text" id = "NewPage" onKeyUp = "value = value. replace (/\ D/g, '')" onafterpaste = "value = value. replace (/\ D/g, '')">

5. Only Chinese characters can be entered.
<Input type = "text" onkeyup = "value = value. replace (/[^ \ u4E00-\ u9FA5]/g,'') ">

6. Only English letters 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,'') ">

7. 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, '')">

8. You can enter only 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 (Note::D In [^ \ d \.] cannot be written as uppercase D, otherwise it becomes 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.

Chinese: 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 ;"

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.