Small issues that js regular expressions are easy to ignore

Source: Internet
Author: User

1. special characters in brackets [] do not need to be escaped, such as [/], [.], [*], and [?]. And [+] can directly match the corresponding characters .*? +. The test result is as follows:

 

Therefore, the/[d.]/regular expression matches a number or character ".", which is equivalent to/[d.]/

Ii. Differences between match () and exec ()

The main difference between the two is that the regular expression sets the GLobal IDEntifier g.

If no identifier is set to G', the result of calling string.match(regexp;is the same as that of calling regexp.exe c (string,

Only the first successful match is given in the result of objective c (string.

The regexp. lastIndex attribute is used to record the starting position of the next matching. The test is as follows:

PS: note that lastIndex only applies to exec () and test () methods. string. match (regexp) does not affect the lastIndex attribute of regexp.

I have added my favorites from the Internet, but they are all excellent products. They are specialized in verifying mobile phone numbers and phone numbers.

The Code is as follows: Copy code
Function checkPhone (phone)
{
// Verify the phone number, including the phone number 153,159
If (phone = ""){
Alert ("the phone number cannot be blank! ");
Return false;
}
If (phone! = ""){
Var p1 =/^ ([0 +] d {2, 3 }-)? (0d {2, 3 })-)? (D {7, 8}) (-(d {3 ,}))? $ /;
Var me = false;
If (p1.test (phone) me = true;
If (! Me ){
// Alert ('Sorry, www. bKjia. c0m. The phone number you entered is incorrect. Use-to separate the area code from the phone number ');
Return false;
}
}
Return true;
}

 
Mobile phone number verification

The Code is as follows: Copy code

String. prototype. isTel = function ()
{
// "Compatible format: Country Code bKjia. c0m (2 to 3 digits)-area code (2 to 3 digits)-telephone number (7 to 8 digits)-extension number (3 digits )"
// Return (/^ ([0 +] d {2, 3 }-)? (0d {2, 3 })-)? (D {7, 8}) (-(d {3 ,}))? $/. Test (this. Trim ()));
Return (/^ ([0 +] d {2, 3 }-)? (0d {2, 3})-) (d {7, 8}) (-(d {3 ,}))? $/. Test (this. Trim ()));
}
// Verify the mobile phone number

String. prototype. isMobile = function (){
Return (/^ (? : 13d | 15 [89])-? D {5} (d {3} | * {3}) $/. test (this. Trim ()));
}

Phone number verification

The Code is as follows: Copy code
Function CheckNum () {// phone Verification
Var InputValue = document. gsjbxxBean. dh. value;
Var reg =/^ ([0-9] | [-]) + $/g;
Var isValid
Isvalid1_reg.exe c (InputValue)
If (! IsValid ){
Return false
}
Return true
}

Check the phone number

The Code is as follows: Copy code

Function isTel (str ){
Var reg =/^ ([0-9] [-]) + $/g;
If (str. length <7 str. length> 18 ){
Return false;
}
Else {
Return reg.exe c (str );
}
}

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.