Javascript mobile phone number validity verification code set _ javascript skills

Source: Internet
Author: User
The following is the code for verifying the phone number in JS. It is very practical and is the first code for everyone:

The Code is as follows:


Function Checkreg ()
{
// Verify the phone number, including the phone number 153,159
If (document. form. phone. value = "" & document. form. UserMobile. value = ""){
Alert ("You must enter at least one phone number and one mobile phone number! ");
Document. form. phone. focus ();
Return false;
}
If (document. form. phone. value! = ""){
Var phone = document. form. phone. value;
Var p1 =/^ ([0 \ +] \ d {2, 3 }-)? (0 \ d {2, 3 })-)? (\ D {7, 8}) (-(\ d {3 ,}))? $ /;
Var me = false;
If (p1.test (phone) me = true;
If (! Me ){
Document. form. phone. value = '';
Alert ('Sorry, the phone number you entered is incorrect. Use-to separate the area code from the phone number ');
Document. form. phone. focus ();
Return false;
}
}
If (document. form. UserMobile. value! = ""){
Var mobile = document. form. UserMobile. value;
Var reg0 =/^ 13 \ d {5, 9} $ /;
Var reg1 =/^ 153 \ d {4, 8} $ /;
Var reg2 =/^ 159 \ d {4, 8} $ /;
Var reg3 =/^ 0 \ d {10, 11} $ /;
Var my = false;
If (reg0.test (mobile) my = true;
If (reg1.test (mobile) my = true;
If (reg2.test (mobile) my = true;
If (reg3.test (mobile) my = true;
If (! My ){
Document. form. UserMobile. value = '';
Alert ('Sorry, the phone number or PHS number you entered is incorrect. ');
Document. form. UserMobile. focus ();
Return false;
}
Return true;
}
}


Description
The test method checks whether a mode exists in the string. If yes, true is returned. Otherwise, false is returned.

Regular Expression section:
\ D represents a number
{7, 8} stands for 7-8 digits (phone number)
{3,} represents the extension number.
D {2, 3} represents the area code
\ +] \ D {2, 3} represents the international area code
^ 13 \ d {5, 9} $ // 130? 139. At least 5 bits, up to 9 bits
/^ 153 \ d {153} $ // China Unicom. At least 4 bits, at most 8 bits
/^ 159 \ d {159} $ // move. At least 4 bits, at most 8 bits

Second:

The Code is as follows:


Var Mobile = $ ("# varMobilePhone"). val ();
Var Phone = $ ("# varPhoneNo"). val ();
If (Mobile = "" & Phone = "")
{
Alert ("mobile phone and fixed phone, please fill in at least one contact method! ");
$ ("# VarMobilePhone"). focus ();
Return;
}
If (Mobile! = "")
{
If (! IsMobil (Mobile ))
{
Alert ("enter the correct mobile phone number! ");
$ ("# VarMobilePhone"). focus ();
Return ;}
}
// Mobile phone number verification information
Function isMobil (s)
{
Var patrn =/(^ 0 {0, 1} 1 [3 | 4 | 5 | 6 | 7 | 8 | 9] [0-9] {9} $ )/;
If (! Patrn.exe c (s ))
{
Return false;
} Return true ;}
Background verification is as follows:
If (model. Zip! = Null)
{
If (! Common. PageValidate. IsValidate (model. Zip, "^ \ d {6} $ "))
{Common. WebMessage. showMsg (HttpContext. Current, "enter the correct zip code ");
Return;
}
}
If (model. PhoneNo! = Null)
{
If (! Common. pageValidate. isValidate (model. phoneNo, "\ d {3}-\ d {8} | \ d {4}-\ d {7 }"))
{
Common. WebMessage. showMsg (HttpContext. Current, "enter the correct phone number! ");
Return;
}
}
If (model. MobilePhone! = Null)
{
If (! Common. pageValidate. isValidate (model. mobilePhone, "^ 0 {0, 1} (13 [0-9] | 15 [3-9] | 15 [0-2] | 18 [0-9]) [0-9] {8} $ "))
{
Common. WebMessage. showMsg (HttpContext. Current, "enter the correct 11-digit valid mobile phone number! ");
Return;
}
}

Related Article

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.