验证身份证号码的js代码(完美支持15与18位身份证)

发布时间:2020-12-05编辑:脚本学堂
又一个验证身份证号码的js代码,支持地区、15位与18位身份证号码的准确校验,有需要的朋友可以参考下。

完整代码如下:
 

复制代码 代码示例:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>验证身份证号码_www.jb200.com</title>
   <script type="text/javascript">
var aCity={11:"北京",12:"天津",13:"河北",14:"山西",15:"內蒙古",21:"遼寧",22:"吉林",23:"黑龍江",31:"上海",32:"江蘇",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山東",41:"河南",42:"湖北",43:"湖南",44:"廣東",45:"廣西",46:"海南",50:"重慶",51:"四川",52:"貴州" ,53:"雲南",54:"西藏",61:"陝西",62:"甘肅",63:"青海",64:"寧夏",65:"新疆",71:"臺灣",81:"香港",82:"澳門",91:"國外"} ;

    function checkCard(sId){ 
        
    var iSum=0
    var info=""
    var result = new Array() ;
  
   if(sId.length!=15 && sId.length!=18)
   {
         result[0] = false ;
        result[1] = "身份證號碼長度錯誤";
        return result;
   }
  
   if(sId.length==15)
   {//15位身份證驗證
        if (isNaN(sId))
        {
            result[0] = false ;
            result[1] = "身份證號碼格式錯誤";
            return result;
        }       
        if(aCity[parseInt(sId.substr(0,2))]==null)
        {
            result[0] = false ;
            result[1] = "非法地區";
            return result ;
        }
        var sBirthday="19"+sId.substr(6,2)+"-"+Number(sId.substr(8,2))+"-"+Number(sId.substr(10,2));
        var d=new Date(sBirthday.replace(/-/g,"/")) ;
        if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))
        {
            result[0] = false ;
            result[1] = "非法生日";

            return result ;

        }
   }
   else
   {//18位身份證驗證 
        if(!/^d{17}(d|x)$/i.test(sId))
        {
            result[0] = false ;
            result[1] = "非身份證號碼";
            return result;
        }
        sId=sId.replace(/x$/i,"a");
        if(aCity[parseInt(sId.substr(0,2))]==null)
        {
            result[0] = false ;
            result[1] = "非法地區";
            return result ;
        }
        var sBirthday=sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2));
        var d=new Date(sBirthday.replace(/-/g,"/")) ;
        if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))
        {
            result[0] = false ;
            result[1] = "非法生日";

            return result ;

        }
        for(var i = 17;i>=0;i --) iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11) ;
        if(iSum%11!=1)
        {
            result[0] = false ;
            result[1] = "非法證號";
            return result ;

        }
    }
     result[0] = true ;
    
     var Sex_Flag = (sId.length==15) ?  sId.substr(14,1) : sId.substr(16,1); //男性為奇數,女性為偶數
     result[1] = "合法證件rnrn證件基本信息為:"+aCity[parseInt(sId.substr(0,2))]+","+sBirthday+","+(Sex_Flag%2?"男":"女") ;     
     
     return result ;
}

 function checkResult(w)
{
    var sId = w.value ;
    if( sId != "")
    {
        var result = checkCard(sId);
       
        if( result[0] == false )
        {
            w.focus();
            alert(result[1]);
           
        }
        else
        {//真正使用時,屏蔽掉該分支即可
            alert(result[1]);
        }
    }   
}
</script>
</head>
<body>
<div style="height:100%;">
<div style="text-align:center; "><span>15或18位身份證號碼驗證</span></div>
<div style="height:90%;"><span>請輸入一個身份證號碼:</span><input  type="text" onblur="checkResult(this)" maxlength="18" /></div>
</div>
</body>
</html>

##==>常用js验证代码大全(Email、手机号码、身份证号码等)