JavaScript Regular expression validation code (recommended) _ Regular expression

Source: Internet
Author: User
Tags regular expression trim

REGEXP: is shorthand for regular expressions (regular expression).

A regular expression describes the pattern object of a character. You can use regular expressions to describe what you want to retrieve.

A simple pattern can be a single character. More complex patterns include more characters and can be used for parsing, format checking, substitution, and more.

Determines whether the input is an empty function IsNull () {var str = document.getElementById (' str '). Value.trim (); if (str.length==0) {alert (' Sorry, the text box cannot be empty or a space! '); 
/Please change the text box to the name of the property you want to verify! 
}//Determine if the date type is YYYY-MM-DD format type function IsDate () {var str = document.getElementById (' str '). Value.trim (); 
if (str.length!=0) {var reg =/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) $/; 
var r = Str.match (reg); if (r==null) alert (' Sorry, the date you entered is not in the correct format! '); 
Please change the "date" to the name of the property you want to verify! 
}//Determine if the date type is a type YYYY-MM-DD hh:mm:ss Format function isdatetime () {var str = document.getElementById (' str '). Value.trim (); 
if (str.length!=0) {var reg =/^ (\d{1,4}) (-|\/) (\d{1,2}) \2 (\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2}) $/; 
var r = Str.match (reg); if (r==null) alert (' Sorry, the date you entered is not in the correct format! '); 
Please change the "date" to the name of the property you want to verify! 
}//Determine if the date type is HH:MM:SS format type function istime () {var str = document.getElementById (' str '). Value.trim (); if (str.length!=0) {reg=/^ (20|21|22|23|[ 0-1]\d) \:[0-5][0-9]) (\:[0-5][0-9])? $/if (!reg.test (str)) {alert ("Sorry, the date you entered is not in the correct format!"); /Please change "date" to the attribute you need to verifyName! 
}}//Determine if the character entered is an English letter function isletter () {var str = document.getElementById (' str '). Value.trim (); 
if (str.length!=0) {reg=/^[a-za-z]+$/; if (!reg.test (str)) {alert ("Sorry, the English letter type you entered is not in the correct format!"); 
/Please change the "English letter type" to the name of the property you need to verify! 
}}//Determine if the character entered is an integer function Isinteger () {var str = document.getElementById (' str '). Value.trim (); 
if (str.length!=0) {reg=/^[-+]?\d*$/; if (!reg.test (str)) {alert ("Sorry, the integer type you entered is not in the correct format!"); 
/Please replace "integer type" with the name of the property you want to verify! 
}}//Determine if the character entered is a double-precision function isdouble (val) {var str = document.getElementById (' str '). Value.trim (); 
if (str.length!=0) {reg=/^[-\+]?\d+ (\.\d+)? $/; if (!reg.test (str)) {alert ("Sorry, the Double-precision type you entered is not in the correct format!"); 
/Please replace "double type" with the name of the property you want to verify! 
}}//Determine if the character entered is: A-z,a-z,0-9 function isstring () {var str = document.getElementById (' str '). Value.trim (); 
if (str.length!=0) {reg=/^[a-za-z0-9_]+$/; if (!reg.test (str)) {alert ("Sorry, the string type you entered is not well-formed!"); 
/Please replace "string type" with the name of the property you want to verify! }}//Determine if the character entered is Chinese function ischinese () {var str = Document.getelemeNtbyid (' str '). Value.trim (); 
if (str.length!=0) {reg=/^[\u0391-\uffe5]+$/; if (!reg.test (str)) {alert ("Sorry, the string type you entered is not well-formed!"); 
/Please replace "string type" with the name of the property you want to verify! 
}//Determine if the email format entered is correct function isemail () {var str = document.getElementById (' str '). Value.trim (); if (str.length!=0) {reg=/^\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] 
\w+) *$/; if (!reg.test (str)) {alert ("Sorry, the string type you entered is not well-formed!"); 
/Please replace "string type" with the name of the property you want to verify! 
The correct function iszip () {var str = document.getElementById (' str ') is judged by the input ZIP code (only six digits). Value.trim (); 
if (str.length!=0) {reg=/^\d{6}$/; if (!reg.test (str)) {alert ("Sorry, the string type you entered is not well-formed!"); 
/Please replace "string type" with the name of the property you want to verify! 
The number entered is not greater than a specific number function MaxValue () {var val = document.getElementById (' str '). Value.trim (); 
if (str.length!=0) {reg=/^[-+]?\d*$/; if (!reg.test (str)) {//Judge whether the number type if (Val>parseint (' 123 '))//"123" sets the maximum value for itself {alert (' Sorry, the number you entered is out of range ');//Please add "number" 
Change to the name of the property you want to verify!  } 
} 
} 
}

Phone:/^ ((\d{2,3}\) | ( \d{3}\-))? (\ (0\d{2,3}\) |0\d{2,3}-)? [1-9]\d{6,7} (\-\d{1,4})? $/

Mobile:/^ ((\d{2,3}\) | ( \d{3}\-))? 13\d{9}$/

URL:/^http:\/\/[a-za-z0-9]+\. [a-za-z0-9]+[\/=\?%\ -&_~ ' @[\]\ ': +!] * ([^<>\ "\"]) *$/

Idcard:/^\d{15} (\d{2}[a-za-z0-9])? $/

QQ:/^[1-9]\d{4,8}$/

A special Amount:/^ ((\d{1,3} (, \d{3}) *) | ( \d+)) (\.\d{2})? $///Description: In addition to the "XXX xx,xxx xx,xxx.00" format

Provide each JS verification method above. Trim () property 
string.prototype.trim=function () {return 
this.replace (/^\s*) | ( \s*$)/g, ""); 

Call:

<input type= "text" name= "str" >
<input type= "button" value= "OK" onclick= "" >//onclick write their own JS validation function to call C2/><script language= "javascript" type= "Text/javascript" >
var patterms = new Object ();
Verify IP
patterms.ip =/^ (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) (\. ( \D{1,2}|1\D\D|2[0-4]\D|25[0-5])) {3}$/;
Verify email
patterms.email =/^[a-za-z0-9_-]+@[a-za-z0-9_-]+ (\.[ a-za-z0-9_-]+) +$/;
Validate date format 2009-07-13
patterms.date =/^\d{4}-(0?[ 1-9]|1[0-2])-(0?[ 1-9]| [1-2]\d|3[0-1]) $/;
Validation time format 16:55:39
patterms.time = new RegExp ("^ ([0-1]\\d|2[0-3]): [0-5]\\d:[0-5]\\d$");
Validation function
Verify (STR,PAT)
{
var thepat;
Thepat = Patterms[pat];
if (Thepat.test (str))
{return
true;
}
else
{return
false;
}
}
Test
alert (Verify ("asidycom@163.com", "email") + "," +verify ("192.168.1.1", "IP") +
"," +verify ("16:55:39", "Time") + "," +verify ("2009-07-13", "date") + "," +verify ("192.168", "IP"));

Verify Number: ^[0-9]*$

Verify the N-bit number: ^\d{n}$

Verify at least n digits: ^\d{n,}$

Verify the number of m-n bits: ^\d{m,n}$

Verify numbers beginning with 0 and non 0: ^ (0|[ 1-9][0-9]*) $

Verify positive real number with two decimal digits: ^[0-9]+ (. [ 0-9]{2})? $

Verify positive real number with 1-3 decimal digits: ^[0-9]+ (. [ 0-9]{1,3})? $

To validate a Non-zero positive integer: ^\+? [1-9] [0-9]*$

Validation of nonzero negative integers: ^\-[1-9][0-9]*$

Verify nonnegative integers (positive integer + 0) ^\d+$

Validates a positive integer (negative integer + 0) ^ ((-\d+) | ( 0+)) $

Verify the character with a length of 3: ^. {3}$

Verify a 26-letter string: ^[a-za-z]+$

Verify a string of 26 uppercase English letters: ^[a-z]+$

Verify a string consisting of 26 lowercase English letters: ^[a-z]+$

Verify a string consisting of numbers and 26 English letters: ^[a-za-z0-9]+$

Verify a string of numbers, 26 English letters, or underscores: ^\w+$

Verify user name or nickname often used: ^[\u4e00-\u9fa5a-za-z0-9-_]*$ only in English, numerals, underscores, minus

Verify user password: ^[a-za-z]\w{5,17}$ the correct format is: Start with a letter, length between 6-18, can only contain characters, numbers and underscores.

Verify that it contains ^%& ',; =?$\ ' and other characters:[^%& ', =?$\x22]+

Verify Chinese characters: ^[\u4e00-\u9fa5],{0,}$

Verify email Address: ^\w+[-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *$

Verify interneturl:^http://([\w-]+\.) +[\w-]+ (/[\w-./?%&=]*) $ ^[a-za-z]+://(w+ (-w+) *) (. w+ (-w+) *) *) * (? s*)? $

Verify phone number: ^ (\ (\d{3,4}\) |\d{3,4}-)? \d{7,8}$:--the correct format is: xxxx-xxxxxxx,xxxx-xxxxxxxx,xxx-xxxxxxx,xxx-xxxxxxxx,xxxxxxx, XXXXXXXX.

Verify ID Number (15-bit or 18-digit number): ^\d{15}|\d{}18$

Verify 12 months of the year: ^ (0?[ 1-9]|1[0-2]) $ The correct format is: "01"-"09" and "1" "12"

Validation one months of 31 days: ^ (0?[ 1-9]) | ((1|2) [0-9]) $ |30|31) $ The correct format is: 01, 09, and 1, 31.

Integer: ^-?\d+$

Non-negative floating-point number (positive floating-point number + 0): ^\d+ (\.\d+)? $

Positive floating-point number ^ ([0-9]+\.[ 0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\. [0-9]+) | ([0-9]*[1-9][0-9]*)] $

Non-positive floating-point number (negative floating-point number + 0) ^ ((-\d+ (\.\d+)?) | (0+ (\.0+)) $

Negative floating-point number ^ (-([0-9]+\.[ 0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*\. [0-9]+) | ([0-9]*[1-9][0-9]*))] $

Floating-point number ^ (-?\d+) (\.\d+)? $

Since the mobile phone number of the continuous update, the previous regular expression has been unable to meet the needs. Rewrite this expression,

var regex = {
Mobile:/^? ( [-]| []| []| [])[-]{}$/
}

Analysis of Expressions:

"/" represents a regular expression.

"^" represents the starting position of the string, and "$" represents the end position of the string.

“?” Represents a match to the previous character one or 0, so here's 0? The cell phone number can start with 0 or not 0.

The next part of the verification of the 11-bit mobile phone number, starting from 13, since 130-139 have so can be the selection between the [0-9],15 number is not 154 so [] There is no 4 this number, of course, can also be written [0-35-9], the following 18 and 14 open the number ibid.

The parentheses enclose a subexpression that contains 4 optional branches, respectively, with a "|" To distinguish between the "|" in the regular. The priority is the lowest, where each branch matches 3 characters (one [] can only match one character, inside is optional meaning), that is, mobile phone number of the first 3 digits, then there are 8 digits to match, can be 0-9 of any character, so is "[0-9]{8}", {} The number in represents the number of characters that match the preceding character.

The above content is small make up to share JavaScript regular expression verification code, hope to help everyone, if you want to know more wonderful content please pay attention to cloud Habitat community website, thank you!

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.