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.