Verify if the input is a mobile phone number or phone number.

Source: Internet
Author: User
The Code is as follows:

<Script type = "text/javascript">
String. prototype. Trim = function (){
Var m = this. match (/^ \ s * (\ S + (\ s + \ S +) *) \ s * $ /);
Return (m = null )? "": M [1];
}

String. prototype. isMobile = function (){
Return (/^ (? : 13 \ d | 15 [89])-? \ D {5} (\ d {3} | \ * {3}) $/. test (this. Trim ()));
}

String. prototype. isTel = function ()
{
// "Compatible format: Country Code (two to three digits)-area code (two to three digits)-telephone number (seven to eight digits)-extension number (three digits )"
// Return (/^ ([0 \ +] \ d {2, 3 }-)? (0 \ d {2, 3 })-)? (\ D {7, 8}) (-(\ d {3 ,}))? $/. Test (this. Trim ()));
Return (/^ ([0 \ +] \ d {2, 3 }-)? (0 \ d {2, 3})-) (\ d {7, 8}) (-(\ d {3 ,}))? $/. Test (this. Trim ()));
}

Function chkForm (){
With (document. form1 ){
If (tel. value. isMobile () | tel. value. isTel ()){
Tel. value = tel. value. Trim ();
Alert ("your phone number/mobile phone number is:" + tel. value );
Return true;
}
Else {
Alert ("enter the correct mobile phone number or phone number \ n For example: 13916752109 or 0712-3614072 ");
Tel. focus ();
Return false;
}
}
}
</Script>
<Form name = "form1" method = "post" action = "">
<Input type = "text" name = "tel" value = "13916752109" size = "15"/>
<Input type = "button" value = "test" onclick = "return chkForm ()"/>
</Form>

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.