Javascript mobile phone number Regular Expression verification function original

Source: Internet
Author: User

Copy codeThe Code is as follows:
Function checkMobile (){
Var sMobile = document. mobileform. mobile. value
If (! (/^ 1 [3 | 4 | 5 | 8] [0-9] \ d {4, 8} $/. test (sMobile ))){
Alert ("the first seven digits of a complete 11-digit mobile phone number or a correct mobile phone number ");
Document. mobileform. mobile. focus ();
Return false;
}
}

The following is a simple analysis of the above regular expression edited by the script house. With the increase in the number segment of the mobile phone, you can expand the function by yourself. If you do not know the regular expression, you should have read this article.
This is the main code. You can modify it later.
^ 1 [3 | 4 | 5 | 8] [0-9] \ d {4, 8} $
^ 1 indicates that the mobile phone number starts with 1. Currently, the Chinese mobile phone number does not start with another one. It may not be necessary in the future.
[3 | 4 | 5 | 8] After 1, it can be a number of 3, 4, 5, or 8. If the phone number starting with 190 is displayed, the following is required: [3 | 4 | 5 | 8 | 9]
[0-9] indicates any number in the middle of 0-9, which can be 0 or 9.
The \ d {} is the same as that of [0-9]. It is a number in the middle of 0-9. {4, 8} indicates matching the first four digits with a maximum of eight digits. Why is it not the 8-bit mobile phone number, because the first seven digits can be used to know the specific address when the phone number is retrieved from the location, and the last four digits will not be affected.
The following is the test code:

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
The following is the code from another website. Let's take a look. In addition, we recommend that you learn regular expressions, which are not applicable in many places.
\ D represents a number
Example: 1, 2, or 3, a single number
If there are multiple, you can use \ d *
\ D {7, 8} represents 7-8 digits (phone number)
Example: 12345678
{7, 8} indicates a 7-or 9-digit number. The general phone number is correct.
\ D {3,} represents the extension number
Example: 123,12345
{3,} indicates a minimum of three numbers. More
0 \ d {2, 3} represents the area code
Example: 021
[0 \ +] \ d {2, 3} represents the international area code
If the minus sign is used for the connection, the complete
/^ ([0 \ +] \ d {2, 3 }-)? (0 \ d {2, 3 })-)? (\ D {7, 8}) (-(\ d {3 ,}))? $/

Test code:
<Input id = txt onchange = "testit ()">
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Getting started with regular expressions in 30 minutes
Http://www.jb51.net/tools/zhengze.html
Basic Regular Expressions
Http://www.jb51.net/article/18526.htm

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.