美文网首页我爱编程
JQuery 身份证号码效验

JQuery 身份证号码效验

作者: 坚果MAN | 来源:发表于2018-04-12 13:54 被阅读82次
    //为值添加0  
        function Append_zore(temp) {  
            if(temp<10)   
                return "0"+temp;  
            else   
                return temp;   
        }      
    
    //身份证号码验证     
    function validateDidentity(certNumber){  
        if(certNumber!="")  
        {     
            //身份证的地区代码对照  
            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: "国外" };           
            //获取证件号码  
            var person_id=certNumber;  
            //合法性验证  
            var sum = 0;  
            //出生日期  
            var birthday;  
            //验证长度与格式规范性的正则  
            var pattern=new RegExp(/(^\d{15}$)|(^\d{17}(\d|x|X)$)/i);         
            if (pattern.exec(person_id)) {  
                //验证身份证的合法性的正则  
                pattern=new RegExp(/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/);  
                if(pattern.exec(person_id)) {     
                    //获取15位证件号中的出生日期并转位正常日期       
                    birthday = "19"+person_id.substring(6,8)+"-"+person_id.substring(8,10)+
                               "-"+person_id.substring(10,12);            
                }                 
                else   
                {     
                    person_id = person_id.replace(/x|X$/i,"a");                       
                    //获取18位证件号中的出生日期  
                    birthday =person_id.substring(6,10)+"-"+person_id.substring(10,12)+
                              "-"+person_id.substring(12,14);  
                      
                    //校验18位身份证号码的合法性  
                    for (var i = 17; i >= 0; i--)   
                    {  
                        sum += (Math.pow(2, i) % 11) * parseInt(person_id.charAt(17 - i), 11);  
                    }  
                    if (sum % 11 != 1) {      
                        $(this).addClass("txtRequired");                  
                        alert("身份证号码不符合国定标准,请核对!");                               
                        $("#certificateNumber").val("");  
                        return;  
                    }             
                }  
                //检测证件地区的合法性                                  
                if (aCity[parseInt(person_id.substring(0, 2))] == null)   
                {  
                    $(this).addClass("txtRequired");  
                    alert("身份证地区未知,请核对!");                             
                    $("#certificateNumber").val("");  
                    return;  
                }  
                var dateStr = new Date(birthday.replace(/-/g, "/"));                  
                if (birthday != (dateStr.getFullYear()+"-"+ Append_zore(dateStr.getMonth()+1)+"-"
                               + Append_zore(dateStr.getDate()))) {  
                    $(this).addClass("txtRequired");  
                    alert("身份证出生日期非法!");                           
                    $("#certificateNumber").val("");    
                    return;  
                }  
                $(this).removeClass("txtRequired");
                //修改证件号码
                $("#dateOfBirth").val(birthday); 
                //带出性别
                 var last = certNumber[certNumber.length - 2];
                 if(last % 2 != 0)
                     $("#sex option[value='1']").attr("selected", true);
                 else
                     $("#sex option[value='2']").attr("selected", true);
            }else  {     
                $(this).addClass("txtRequired");      
                alert("身份证号码格式非法!");                           
                $("#certificateNumber").val("");  
                return;  
            }             
        }  
    }
    

    相关文章

      网友评论

        本文标题:JQuery 身份证号码效验

        本文链接:https://www.haomeiwen.com/subject/ibvxkftx.html