Jquery 对 身份证号码的验证 (15/18位)

//欢迎验证、给予指正错误,从好多地方查来的资料修改的...已用本人身份证验证...  由于以前修改的问题无法验证带 X 的身份证号码,已修复!   

  

  

//为值添加0   

            function Append_zore(temp)  

            {  

                if(temp<10)   

                {  

                   return "0"+temp;  

               }  

                else   

                {  

                   return temp;  

                }  

            }      

  

//身份证号码验证      

            $("#person_id").change(function(){  

                if($("#person_id").val()!="")  

                {     

                    //身份证的地区代码对照   

                    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=$("#person_id").val();  

                    //合法性验证   

                    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("身份证号码不符合国定标准,请核对!");                               

                                //$("#person_id").val("");   

                                $("#birthday").val("")                    

                                return;  

                            }             

                        }  

                        //检测证件地区的合法性                                   

                        if (aCity[parseInt(person_id.substring(0, 2))] == null)   

                        {  

                           $(this).addClass("txtRequired");  

                            alert("证件地区未知,请核对!");                             

                            //$("#person_id").val("");   

                            $("#birthday").val("");               

                            return;  

                        }  

                        var dateStr = new Date(birthday.replace(/-/g, "/"));                  

                          

                       //alert(birthday +":"+(dateStr.getFullYear()+"-"+ Append_zore(dateStr.getMonth()+1)+"-"+ Append_zore(dateStr.getDate())))   

                       if (birthday != (dateStr.getFullYear()+"-"+ Append_zore(dateStr.getMonth()+1)+"-"+ Append_zore(dateStr.getDate()))) {  

                            $(this).addClass("txtRequired");  

                          alert("证件出生日期非法!");                           

                            //$("#person_id").val("");   

                            $("#birthday").val("");                   

                            return;  

                        }  

                        $(this).removeClass("txtRequired");  

                        $("#birthday").val(birthday);                 

                    }  

                   else  

                    {     

                        $(this).addClass("txtRequired");      

                        alert("证件号码格式非法!");                           

                        //$("#person_id").val("");   

                        $("#birthday").val("")    

                       return;  

                    }             

                }  

                else  

                {  

                    alert("请输入证件号!");         

                    $("#birthday").val("");  

                }  

            });  

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值