商务合作:179001057@qq.com

客户端校验是否为正确的电话号码

技术2022-05-12  0


某平台价值19860元的编程课程资料免费领取【点我领取】


代码如下:   < 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(){     // "兼容格式: 国家代码(2到3位)-区号(2到3位)-电话号码(7到8位)-分机号(3位)"     // 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( " 您的电话/手机号码是: " + tel.value);             return true ;           }          else {               alert( " 请输入正确的手机号码或电话号码/n/n例如:13916752109或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 = " 测 试 "    onclick = " return chkForm() " />    </ form >   较常用的校验代码也一起附上,供大家参考参考: // JavaScript Document //检验用户名是否符合要求 function usernamecheck(){  var username=document.getElementById("username").value;   if(username==""){   document.getElementById("username1").innerText = "此项不能为空!";   return false;  }else   if(/^/w+$/ig.test(username)){    document.getElementById("username1").innerText="您已正确输入!";    return true;   }   else{    document.getElementById("username1").innerText="只能为英文和数字,请重新输入!";   } } //检验密码是不符合要求 function passwordcheck(){  var password=document.getElementById("password").value;   if(password==""){   document.getElementById("password1").innerText = "此项不能为空!";  }else   if(/^/w+$/ig.test(password)&&password.length>=6&&password.length<=30){    document.getElementById("password1").innerText="您已正确输入!";   }   else{    document.getElementById("password1").innerText="只能为英文和数字,长度为6至30!";   } } //对邮箱地址进行校验 function emailcheck(){  var mail=document.getElementById("email").value;  if(mail==""){   document.getElementById("email1").innerText = "此项不能为空!";  }else   if(isEmail(mail)){    document.getElementById("email1").innerText="您已正确输入!";   }   else{    document.getElementById("email1").innerText="格式不对!";   } } //检验两次密码是否一致 function same(){  var password=document.getElementById("password").value;  var password2=document.getElementById("password2").value;  if(password2==""){   document.getElementById("password3").innerText = "此项不能为空!";  }else   if(password==password2){    document.getElementById("password3").innerText="您已正确输入!";   }   else{    document.getElementById("password3").innerText="两次输入的密码不一致!";   } } //邮箱地址校验函数 function isEmail(strEmail) { if (strEmail.search(/^/w+((-/w+)|(/./w+))*/@[A-Za-z0-9]+((/.|-)[A-Za-z0-9]+)*/.[A-Za-z0-9]+$/) != -1) return true; else return false; } //真实姓检验 function realnamecheck(){  var realname=document.getElementById("realname").value;  if(realname==""){   document.getElementById("realname1").innerText = "此项不能为空!";  }else{   document.getElementById("realname1").innerText = "您已正确输入!"; } } //电话格式检验 function phonecheck(){  var phone=document.getElementById("phone").value;  if(phone==""){   document.getElementById("phone1").innerText = "此项不能为空!";  }else   if(isPhone(phone)){    document.getElementById("phone1").innerText="您已正确输入!";   }   else{    document.getElementById("phone1").innerText="格式不对!";   } } //电话检验函数 function isPhone(v){         var reg=/((15[89])/d{8})|((13)/d{9})|(0[1-9]{2,3}/-?[1-9]{6,7})/i;         if(reg.test(v)){             return true;         }         else{return false;}     }   function chkForm(objForm){  var username=objForm.username.value;  var password=objForm.password.value;  var password2=objForm.password2.value;  var realname=objForm.realname.value;  var email=objForm.email.value;  var phone=objForm.phone.value;  if(username==""||!/^/w+$/ig.test(username)){   alert("【用户名】不能为空或格式不符合规范/n");   objForm.username.focus();   return false;  }  if (password == "" || (password.length < 6 || password.length > 20))  {   alert("【密码】不能为空或长度不符合规范/n");   objForm.password.focus();   return false;  }  if(password!=password2){   alert("【确认密码】与密码不一致/n");   objForm.password2.focus();   return false;  }  if(realname==""){   alert("【真实姓名】不能为空或格式不符合规范/n");   objForm.realname.focus();   return false;  }  if(!isEmail(email)||email==""){   alert("【邮箱】不能为空或格式不符合规范/n");   objForm.email.focus();   return false;  }  if(!isPhone(phone)||phone==""){   alert("【电话】不能为空或格式不符合规范/n");   objForm.phone.focus();   return false;  }  return true; } function reg_sub() {  var obj =document.getElementById('Form');  if(!chkForm(obj))  {   return false;  }  obj.submit(); }

最新回复(0)