Latest JavaScript code to verify phone numbers

Source: Internet
Author: User
Tags trim

Example

The code is as follows Copy Code
var tel = 18767802354;
var reg =/^0?1[3|4|5|8][0-9]\d{8}$/;
if (Reg.test (tel)) {
Alert ("Correct number ~");
}else{
Alert ("Wrong number ~");
};

Match the phone number at the beginning of 13,14,15,18!

Example

Verify mobile phone Number one

The code is as follows Copy Code

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

var mobile =/^ ((d{3}) | ( d{3}-))? 13d{9}|15[0-9]d{8}$/

Regular expression section:

D Represents a number
{7,8} represents 7-8 digits (indicates phone number)
{3,} represents an extension number
d{2,3} represents the area code
+]d{2,3} represents the international area code
^13d{5,9}$///130–139. At least 5 digits, up to 9 digits
/^153d{4,8}$///Unicom 153. At least 4 digits, up to 8 digits
/^159d{4,8}$///Mobile 159. At least 4 digits, up to 8 digits

Example, the above several are limited to the difference, the first three digits if there is a new number may not be able to correctly judge, the following for everyone to be a number one is 1, the second is 3 to 8 of the numbers, a total of 11


We get the corresponding regular expression is: 1[3-8]+\d{9}. Here is the function to verify the phone number.

  code is as follows copy code
 var Str=utel.value;
 var Errors=document.getelementbyid ("Myerror");
 var regpartton=/1[3-8]+\d{9}/;
 if (!str | | str==null) {
  erros.innerhtml= phone number cannot be empty! ";
  utel.focus ();
  return false;
 }else if (!regpartton.test (str)) {
  errors.innerhtml= phone number is not in the correct format! ";
  utel.focus ();
  return false;
 }else{
  errors.innerhtml.nodevalue= "";
  return true;
 
}

Analysis: From the above example, the core code is a regular expression of the 1[3-8]+\d{9} just writing a different way but it has reached the effect we want.

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.