CopyCode The 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 regular expression in the above section. With the increase in the number segment of the mobile phone, you can expand the function by yourself, after reading this Article , a friend who does not understand regular expressions should be able to read it.
This code is mainly used, you can modify this section later.
^ 1 [3 | 4 | 5 | 8] [0-9] \ D {4, 8 }$
^ 1 indicates 1 start, the Chinese mobile phone number does not start with another one. It may not be necessary in the future.
[3 | 4 | 5 | 8] follows 1 above, it can be a number of 3, 4, 5, or 8. If the phone number starting with 190 is displayed, the following [3 | 4 | 5 | 8 | 9]
[0-9] indicates any number in the middle of 0-9, it can be 0 or 9
\ D {4, 8}. This \ D is the same as [0-9] and 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: <br/> <p>
[Ctrl + A select all Note: To introduce external JS, refresh it before execution]
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