日期:2014-05-16  浏览次数:20365 次

JS—判断手机号和邮政编码

JS—判断手机号和邮政编码
----------------------------------------------
<form id="form1" name="form1" method="post" action="SurveySave.asp?action=add" onsubmit="return CheckInput();" >
电话/手机:<input class="input_o" type="text" name="txtMobile" id="txtMobile" /><br />
邮政编码:<input class="input_o" type="text" name="txtZipCode" id="txtZipCode" />
<input class="input1" type="submit" name="Submit" id="Submit" value="提&nbsp;&nbsp;交" />
</form>


<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()
{
    return (/^(\d{7,8})(-(\d{3,}))?$/.test(this.Trim()));
}

//alert的显示
function CheckInput()
{
var StrInfo="";

var StrUserMobile="";
var MobileValue=document.getElementById("txtMobile").value;
if(MobileValue.length>0)
{
     if (MobileValue.isMobile()||MobileValue.isTel()) { }
     else
            {
                alert("请输入正确的手机号码或电话号码\n\n例如:13916752109或");
                document.getElementById("txtMobile").focus();
                return false;
            }         
}
else
     StrUserMobile+="请填写电话/手机!";
if(StrUserMobile!="")
     StrInfo+=StrUserMobile;


   if(document.getElementById("txtZipCode").value.length==0)
     StrInfo+="请输入您的邮编!";
   else
   {
      if(document.getElementById("txtZipCode").value.length<6)
          StrInfo+="邮编格式不正确!";
      else
      {
       var rexTel=/^[0-9]+$/;
       if(document.getElementById("txtZipCode").value.length>6 && rexTel.test(document.getElementById("txtZipCode").value))
            StrInfo+="邮编格式不正确!"
      }
   }

if(StrInfo=="")
     return true;
else
{
      alert(StrInfo);
      return false;
}
</script>