JS Verify the legality of the phone number code

Source: Internet
Author: User
Tags regular expression

The positive of the cell phone number verification is:

The code is as follows Copy Code


var reg =/^0{0,1} (13[0-9]|15[7-9]|153|156|18[7-9]) [0-9]{8}$/;

var reg =/^0{0,1} (13[0-9]|15[0-9]) [0-9]{8}$/;

There is also a way of writing

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.


Example 1

The code is as follows Copy Code

function Ismobel (value)

{

if (/^13d{9}$/g.test (value) | | (/^15[0-35-9]d{8}$/g.test (value)) | | (/^18[05-9]d{8}$/g.test (value))

{return true;}

Else

{return false;}
}

Example 2

The code is as follows Copy Code

function Checkchinamobileid (MOBILE) {

if (mobile== "") {
Alert ("Please fill in your mobile number!") ");
return false;
}
if (isNaN (mobile) | | (mobile.length!=11)) {
Alert ("Mobile phone number is 11 digits!") Please fill in the correct! ");
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 number is not a mobile number, please re-enter");

return false;
}
Alert ("Big uncle, this really mobile phone number");
return true;
}
function Checkismobile (MOBILE) {

if (mobile== "") {
Alert ("Please fill in your mobile number!") ");
return false;
}
if (isNaN (mobile) | | (mobile.length!=11)) {
Alert ("Mobile phone number is 11 digits!") Please fill in the correct! ");
return false;
}

var reg =/^0{0,1} (13[0-9]|15[0-9]) [0-9]{8}$/;
if (!reg.test (mobile))
{
Alert ("Your cell phone number is incorrect, please re-enter");

return false;
}
Alert ("Big uncle, this is really mobile phone number, is where I do not know");
return true;
}

Another regular cell phone number method

The code is as follows Copy Code


<script type= "Text/javascript" >
function Enterevent () {
Key=window.event.keycode;
if (key==0xd) {//Determine if the ENTER key is pressed
Mobile_click ();
}

}
function CM (m) {
if (M.length > 11) {
return false;
}else{
var t=/^ (13d{9}) | (15d{9}) | (18d{9}) | (0d{10,11}) $/;
Return T.test (m);
}
}
Window.onload=function () {
var C=document.getelementbyid ("MB");
C.focus ();
}
function Mobile_click () {
var C=document.getelementbyid ("MB");
if (! CM (C.value)) {
Alert ("Please enter a correct cell phone number.") ");
C.select ();
}else{
document.getElementById ("Inputcall"). style.display= "None";
document.getElementById ("Show"). style.display= "Block";
document.getElementById ("Iframediv"). style.display= "Block";

}
}
</script>

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.