Mobile phone number regular expression explanation

Source: Internet
Author: User
Tags regular expression

Regular expression of phone number (mobile phone number, 3-4 area code, 7-8 live video number, 1-4 extension code)

The code is as follows: Copy code
(D {11}) | ^ (d {7, 8}) | (d {4} | d {3})-(d {7, 8 }) | (d {4} | d {3})-(d {7, 8 }) -(d {4} | d {3} | d {2} | d {1}) | (d {7, 8 }) -(d {4} | d {3} | d {2} | d {1}) $)
 

Matching format:
 
11-digit mobile phone number
3-4-bit area code, 7-8 live video numbers, 1-4 ext.
Regular expression -- verify mobile phone number: 13 [0-9] {9}
If the mobile phone number is 86 or + 86 before it is implemented: ^ (+ 86) | (86 ))? (13) d {9} $

Example

The code is as follows: Copy code

$ Mobilephone = trim ($ _ POST ["smMobilePhone"]);
// Regular verification of mobile phone numbers
If (preg_match ("/^ 13 [0-9] {1} [0-9] {8} $ | 15 [0189] {1} [0-9] {8} $ | 189 [0-9] {8} $ /", $ mobilephone )){
// Verification passed
      
} Else {
// Incorrect mobile phone number format
      

The above is our previous method. Next we will introduce the latest regular expression for mobile phone numbers.

The current mobile phone number is increased by 150,153,156,158,159,157,188,189.
Therefore, the regular expression is as follows: string s = @ "^ (13 [0-9] | 15 [0 | 3 | 6 | 7 | 8 | 9] | 18 [8 | 9]) d {8} $ ";


Tips of Wan Yahu, adding 180,147, etc., more comprehensive expressions:

The code is as follows: Copy code

^ (1 ([35] [0-9]) | (47) | [8] [0126789]) d {8} $

 

Modify the 183 and add the landline as follows:

The code is as follows: Copy code

Mobile phone: ^ (1 ([35] [0-9]) | (47) | [8] [01236789]) d {8} $

Landline: ^ 0d {2, 3 }(-)? D {7, 8} $

Asp.net practices

The code is as follows: Copy code

Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Text. RegularExpressions;
Using System. Windows. Forms;

Namespace ConsoleApplication1
{
Class Program
    {
Static void Main (string [] args)
        {
// String s = @ "^ (13 [0-9] | 15 [0 | 3 | 6 | 8 | 9]) d {8} $ ";

String s = @ "^ (13 [0-9] | 15 [0 | 3 | 6 | 7 | 8 | 9] | 18 [8 | 9]) d {8} $ ";
While (true)
            {
String input = Console. ReadLine ();
If (Regex. IsMatch (input, s ))
                {
MessageBox. Show ("Exactly! ");
                }
Else
                {
MessageBox. Show ("No! ");
                }
            }
        }
    }
}

A complete instance

You can use the first or second mobile phone number. I don't know if there are more than 15 mobile phone numbers ..

The code is as follows: Copy code

<Input type = "text" id = "mobile" name = "mobile" onblur = "if (value = '') {value = 'Enter your mobile phone number'} "onfocus =" if (value = 'Enter your mobile phone number ') {value = ''}" value = "enter your mobile phone number"/>
   
<Input type = "button" name = "china_mobile" value = "check whether the mobile phone number is" onclick = "CheckChinaMobileID (document. getElementById ('mobile'). value)"/>
   
<Input type = "button" name = "is_mobile" value = "verify whether the mobile phone number is" onclick = "CheckIsMobile (document. getElementById ('mobile '). value) "/> function CheckChinaMobileID (mobile ){
       

If (mobile = ""){
Alert ("Enter your mobile phone number! ");
Return false;
        }
If (isNaN (mobile) | (mobile. length! = 11 )){
Alert ("The mobile phone number is 11 digits! Please enter it correctly! ");
Return false;
        }
Var reg =/^ 0 {0, 1} (13 [4-9] | 15 [7-9] | 15 [0-2] | 18 [7-8]) [0-9] {8} $ /;
If (! Reg. test (mobile ))
        {
Alert ("Your mobile phone number is not a mobile number. Please enter it again ");
           
Return false;
        }
Alert ("Grandpa, this is the mobile phone number ");
Return true;
    }

Function CheckIsMobile (mobile ){
       
If (mobile = ""){
Alert ("Enter your mobile phone number! ");
Return false;
        }
If (isNaN (mobile) | (mobile. length! = 11 )){
Alert ("The mobile phone number is 11 digits! Please enter it correctly! ");
Return false;
        }
       
Var reg =/^ 0 {0, 1} (13 [0-9] | 15 [0-9]) [0-9] {8} $ /;
If (! Reg. test (mobile ))
        {
Alert ("Your mobile phone number is incorrect. Please enter it again ");
           
Return false;
        }
Alert ("Grandpa, this is really a mobile phone number. I don't know where it is ");
Return true;
    }

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.