Latest phone number verify regular expression JS code

Source: Internet
Author: User


Use: Determine the cell phone number in the input box

Input: S: string
(1) The telephone number shall consist of numbers, "(") "and"-"
(2) Phone number is 3 to 8 digits
(3) If the phone number contains an area code, then the area code is three digits or four digits
(4) The area code is separated by "(", ")" or "-" and other parts
(5) Mobile phone number is 11 or 12 digits, if 12 digits, then the first digit is 0
(6) The first and second digits of the 11-digit mobile phone number are "13"
(7) The second and third digits of the 12-digit mobile phone number are "13"
According to these rules, you can go with 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: False if return true through validation

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;
}
}

Later, when I manually tested, I found that like 153,152,187,188 can not pass, and then changed 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}$/}

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. Analysis completed.

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.