Today in the development of their own suddenly need to use the JS phone number or phone numbers to verify, I Baidu, found two relatively easy to understand and good regular verification of the code.
Function Name: Istelephone
function: fixed, mobile phone number check function, valid return True, otherwise, return false
function parameters: obj, number to check
Check rule:
(1) The telephone number shall consist of numbers, "(") "and"-"
(2) Phone number is 3 to 8 digits
(3) If the phone number contains an area code, then the area code is three digits or four digits
(4) The area code is separated by "(", ")" or "-" and other parts
(5) Mobile phone number is 11 or 12 digits, if 12 digits, then the first digit is 0
(6) The first and second digits of the 11-digit mobile phone number are "13"
(7) The second and third digits of the 12-digit mobile phone number are "13"
********************/
function Istelephone (obj)//Regular judgment
{
var pattern=/(^[0-9]{3,4}-[0-9]{3,8}$) | (^[0-9]{3,8}$) | (^ ([0-9]{3,4}) [0-9]{3,8}$] | (^0{0,1}13[0-9]{9}$)/;
if (pattern.test (obj))
{
return true;
}
Else
{
return false;
}
}
function Isphonenumber (str) non-regular judgment
{
var I,strlengh,tempchar;
Str=cstr (str);
if (str== "") return false;
Strlength=str.length;
for (i=0;i<strlength;i++)
{
Tempchar=str.substring (i,i+1);
if (!) ( tempchar==0| | tempchar==1| | tempchar==2| | tempchar==3| | tempchar==4| | tempchar==5| | tempchar==6| | tempchar==7| | tempchar==8| |tempchar==9| | tempchar== ';-';))
{
Alert ("Phone numbers can only be entered in numbers and dashes");
return (false);
}
}
return (true);
}
Example Two
function Checkreg ()
{
Verify the phone number of the phone number, including section 153,159
if (document.form.phone.value== "" && document.form.usermobile.value== "") {
Alert ("Phone number and cell phone number at least choose one!") ");
Document.form.phone.focus ();
return false;
}
if (Document.form.phone.value!= "") {
var Phone=document.form.phone.value;
var p1 =/^ ([0+]d{2,3}-)? ( 0d{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 wrong. Please use-split ' between area code and telephone number;
Document.form.phone.focus ();
return false;
}
}
if (Document.form.UserMobile.value!= "") {
var Mobile=document.form.usermobile.value;
var reg0 =/^13d{5,9}$/;
var reg1 =/^153d{4,8}$/;
var reg2 =/^159d{4,8}$/;
var reg3 =/^0d{10,11}$/;
var i = 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, you have entered the phone or PHS number is wrong. ');
Document.form.UserMobile.focus ();
return false;
}
return true;
}
}
Description
The test method checks whether a pattern exists in the string, returns True if it exists, or returns false.
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