JavaScript is based on regular expression text box validation code _ Regular Expression

Source: Internet
Author: User

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

2, you can only input English and digital
<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 and numbers, underscores, and dots. And the beginning can only be underlined and letters.
/^ ([A-za-z_]{1}) ([\w]*) $/g.test (str)

4, you can only enter a number
<input name= "text" type= "text" id= "NewPage" onkeyup= "Value=value.replace" (/\d/g, "" onafterpaste= "value=") Value.replace (/\d/g, ")" >

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

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

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

8, only allowed to enter English, and can not paste or pop-up paste menu
<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;"

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