JavaScript-common regular expression form validation code _ regular Expressions

Source: Internet
Author: User
Common regular expressions are as follows:
Copy Code code as follows:

"^-? [1-9]\\d*$],//integer
"^[1-9]\\d*$",//Positive integer
"^-[1-9]\\d*$",//Negative integer
"^([+-]?) \\d*\\.? \\d+$ ",//number
"^[1-9]\\d*|0$",//positive number (positive integer + 0)
"^-[1-9]\\d*|0$",//negative (negative integer + 0)
"^([+-]?) \\d*\\.\\d+$ ",//floating-point numbers
"^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*$",//Positive floating-point numbers
"^-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*) $",//negative floating-point number
"^-? ([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?. 0+|0) $ ",//floating-point number
"^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?" he said. 0+|0$ ",//nonnegative floating-point number (positive floating-point number + 0)
"^ (-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*)) |?" 0+|0$ ",//non-positive floating-point number (negative floating-point number + 0)
"^\\w+ (-\\w+) | ( \\.\\w+)) *\\@[a-za-z0-9]+ (\\.| -) [a-za-z0-9]+) *\\. [a-za-z0-9]+$],//Mail
"^[a-fa-f0-9]{6}$",//Color
"^http[s]?:\ \/\\/([\\w-]+\\.) +[\\w-]+ ([\\w-./?%&=]*)? $ ",//url
"^[\\ a-\\ 龥 \\?-\\?] +$ ",//Only Chinese
"^[\\x00-\\xff]+$",//only acsii characters
"^\\d{6}$",//ZIP code
"^ (13|15|18) [0-9]{9}$",//Mobile phone
"^ (25[0-5]|2[0-4]\\d| [0-1]\\d{2}| [1-9]?\\d] \. (25[0-5]|2[0-4]\\d| [0-1]\\d{2}| [1-9]?\\d] \. (25[0-5]|2[0-4]\\d| [0-1]\\d{2}| [1-9]?\\d] \. (25[0-5]|2[0-4]\\d| [0-1]\\d{2}| [1-9]?\\d) $ ",//IP address
"^\\s+$",//Non-empty
"(.*)\\. (JPG|BMP|GIF|ICO|PCX|JPEG|TIF|PNG|RAW|TGA) $ ",//Picture
"(.*)\\. (rar|zip|7zip|tgz) $ ",//Compressed file
"^\\d{4}" (\\-|\\/|\.) \\d{1,2}\\1\\d{1,2}$ ",//Date
"^[1-9]*[1-9][0-9]*$",//qq number
"^ ([0\\+]\\d{2,3}-)?" (0\\d{2,3})? (\\d{7,8}) (-(\\d{3,}))? $ ",//Phone number function (including verification of domestic area code, international area code, extension number)
"^\\w+$",//for user registration. Match a string of numbers, 26 English letters, or underscores
"^[a-za-z]+$",//Letter
"^[a-z]+$",//capital letters
"^[a-z]+$",//lowercase letters
"^[1-9] ([0-9]{14}|[ 0-9]{17}) $ "//ID Card



The following is a simple example:
Copy Code code as follows:

function Chknum (obj) {
var val = obj.val;
if (val!= "") {
if (!) ( /^[0-9]{1,20}$/.exec (val))) {
Obj.val= "";
Alert ("Please enter a valid number!");
Obj.focus ();
}
}
}

Two additional sites recommended by the cloud communities:
JavaScript Form Validation Regular expression encyclopedia [recommended]
Example code for JAVASCRIPT using regular expressions for form validation
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.