Javascript learning notes (5) Regular Expressions

Source: Internet
Author: User

Frequently Used metacharacters include:
• Find a single character, except for line breaks and line Terminators;
• \ W matches letters, Chinese characters, numbers, and underlines;
• \ S matches blank characters (including spaces and tabs );
• \ D matches numbers;
• \ B matches the match at the beginning or end of a word;
Common quantifiers include:
• ^ N matches any string starting with n;
• N $ matches any string ending with n;
• N + matches any string containing at least one n;
• N * matches any string containing zero or more n characters;
• N? Matches any string containing zero or one n;
• N {X} matches strings that contain X n sequences;
• N {X, Y} matches strings that contain X or Y n sequences;
A simple example is used to verify the mobile phone number, phone number, and email address:
Javascript code: Copy codeThe Code is as follows: function isMobile (){
Var mobile = document. getElementById ("mobile_phone ");
Var num = mobile. value;
Var reg =/^ (13 [0-9] | 186 | 188 | 150 | 151 | 158 | 159 | 147) \ d {8} $ /;
If (num = ""){
Alert ("Enter the complete mobile phone number ");
Mobile. focus ();
Return false;
} Else if (reg. test (num )){
Alert ("the entered mobile phone number is in the correct format ");
} Else {
Alert ("enter the correct 11-digit mobile phone number ");
Mobile. focus ();
Return false;
}
}
Function isEmail (){
Var email = document. getElementById ("email ");
Var email_value = email. value;
If (email_value = ""){
Alert ("Enter the complete email address ");
Email. focus ();
Return false;
} Else {
Var reg =/^ [a-zA-Z0-9] (\ w) + @ (\ w) + (\.) + (com | com \. cn | net | cn | net \. cn | org | biz | info | gov \. cn | edu \. cn) $ /;
If (reg. test (email_value )){
Alert ("the entered email address format is correct ");
} Else {
Alert ("enter the correct email format ");
Email. focus ();
Return false;
}
}
}
Function isPhone (){
Var phone = document. getElementById ("phone ");
Var phone_value = phone. value;
If (phone_value = ""){
Alert ("Enter the complete landline number ");
Phone. focus ();
Return false;
} Else {
Var reg =/^ [(]? 0 \ d {2, 3} [)]? \ S * [-]? \ S * \ d {87989898} $/; // The format of 010-01098989898 0712 (8989898) 23343434 010-landline numbers all meet
If (reg. test (phone_value )){
Alert ("the entered landline number is correct ");
} Else {
Alert ("Incorrect landline number format ");
Phone. focus ();
Return false;
}
}
}

Some HTML code:Copy codeThe Code is as follows: <p>
<Label for = "mobile_phone"> mobile phones </label>
<Input type = "text" id = "mobile_phone", name = "mobile_phone" value = "15107105287" type = "codeph" text = "/codeph"/>
<Input type = "button" value = "validate" onclick = "isMobile ()"/>
</P>
<P>
<Label for = "phone"> seat machine </label>
<Input type = "text" id = "phone" name = "phone" value = "027-87767676"/>
<Input type = "button" value = "validate" onclick = "isPhone ()"/>
</P>
<P>
<Label for = "email"> email box </label>
<Input type = "" id = "email" name = "email" value = "zhangchen2397@126.com"/>
<Input type = "button" value = "validate" onclick = "isEmail ()"/>
</P>

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.