<script language=JavaScript>
function checkform()
{
  var strIdentityID
  strIdentityID=document.input.IdentityID.value  
  if(isNaN(strIdentityID))
  {
    alert("身份证号必须由数字组成!");
    document.input.IdentityID.focus();
    return false;
  }
  if(strIdentityID.length<15)
  {
    alert("身份证号位数不足15位!");
    document.input.IdentityID.focus();     
    return false;
  } 
  if(strIdentityID.length>15&&strIdentityID.length<18)
  {
    alert("身份证号位数大于15位但不足18位!");
    document.input.IdentityID.focus();    
    return false;
  }
  if(strIdentityID.length>18)
  {
    alert("身份证号位数超过18位!");
    document.input.IdentityID.focus();
    return false;
  }
  return true;
}
</script>

解决方案 »

  1.   

    Ok解决<html>
    <head>
    <script language="javascript">
    function checkform()
    {
     var card
     card=document.myform.idcard.value.length;
     if(card!=15 && card!=18)
      {
       alert("你输入的身份证号码不足15位或18位");  }
    }</script>
    </head>
    <body>
    <form name="myform">
    <input type="text" name="idcard" maxlength="18" onblur="checkform()">
    </form>
    </body>
    </html>
      

  2.   

    //检查身份证:15位或18位
    function checkIdCard(num){
    if (isNaN(num)) {alert("输入的不是数字!"); return false;}
    var len = num.length, re; 
    if (len == 15){
    re = new RegExp(/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{3})$/);
    }else if (len == 18){
    re = new RegExp(/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})(\d)$/);
    }else {
    alert("输入的数字位数不对!"); 
    return false;
    }
    var a = num.match(re);
    if (a != null)  {
    if (len==15) {
    var D = new Date("19"+a[3]+"/"+a[4]+"/"+a[5]);
    var B = D.getYear()==a[3]&&(D.getMonth()+1)==a[4]&&D.getDate()==a[5];
    }else {
    var D = new Date(a[3]+"/"+a[4]+"/"+a[5]);
    var B = D.getFullYear()==a[3]&&(D.getMonth()+1)==a[4]&&D.getDate()==a[5];
    }
    if (!B) {alert("输入的身份证号 "+ a[0] +" 里出生日期不对!"); return false;}
    }
    return true;
    }