I found a lot of regular expressions for mobile phone number verification on the Internet, but they are not comprehensive. Today I verified the regular expressions for mobile phone number verification in 153, but the Verification Failed. Now I have reorganized some of the latest Regular Expressions for mobile phone number verification, for more information, see.
Purpose: Determine the mobile phone number in the input box
Input: s: String
(1) Telephone Numbers consist of numbers, "(", ")", and "-"
(2) 3 to 8 phone numbers
(3) If the telephone number contains a district code, the district code is three or four digits.
(4) area codes are separated by "(", ")" or "-" and other sections.
(5) the mobile phone number is 11 or 12 digits. If the mobile phone number is 12 digits, the first digit is 0.
(6) The first and second digits of 11 mobile phone numbers are "13"
(7) The second and third digits of the 12-digit mobile phone numbers are "13"
Based on these rules, you can use the following regular expressions:
(^ [0-9] {3, 4}-[0-9] {3, 8} $) | (^ [0-9] {3, 8} $) | (^ ([0-9] {3, 4}) [0-9] {3, 8} $) | (^ 0 {0, 1} 13 [0-9] {9} $)
Return: If the verification succeeds, true is returned; otherwise, false is returned.
The Code is as follows: |
Copy code |
Function istel (s ){ Var str = s; Var reg =/(^ [0-9] {3, 4}-[0-9] {3, 8} $) | (^ [0-9] {3, 8} $) | (^ ([0-9] {3, 4}) [0-9] {3, 8} $) | (^ 0 {0, 1} 13 [0-9] {9} $ )/; If (reg. test (str )){ Return true; } Else { Return false; } }
|
When I manually tested it, I found that something like 153,152,187,188 could not pass, and then changed it to the following:
The Code is as follows: |
Copy code |
Var regex = {mobile:/^ 0? (13 [0-9] | 15 [012356789] | 18 [0236789] | 14 [57]) [0-9] {8} $ /} |
Expression Analysis:
"/" Represents a regular expression.
"^" Indicates the start position of the string, and "$" indicates the end position of the string.
"?" It indicates matching one or zero characters before, so here 0? The mobile phone number can start with 0 or not.
Next, verify the 11-digit mobile phone number starting from 13. The range is [0-9] Because the 11-digit mobile phone number is available from 130-139. the number starting with 15 is not 154, so [] does not contain the number 4. Of course, it can also be written as [0-35-9]. The numbers listed in the 18 and 14 below are the same as those listed in the preceding figure.
Enclosed in parentheses represents a subexpression, which contains four optional branches separated by "|". In regular expressions, "|" has the lowest priority, here, each branch matches three characters (one [] can only match one character, which is optional), that is, the first three digits of the mobile phone number, then there are 8 digits to be matched, which can be any 0-9 character. Therefore, the value is "[0-9] {8}", and the number in {} indicates the number that matches the previous character. The analysis is complete.