全国统一服务热线:400-633-9193

身份证验证代码

admin     未知     2017-02-11    1439
function isChinaIDCard(StrNo)
...{
    StrNo = JHshLTrim(StrNo);
    StrNo = JHshRTrim(StrNo);
    StrNo = StrNo.toString();
//    alert(StrNo.length)
    if (StrNo.length == 18)
    ...{
        var a, b, c;
        if (!isInteger(StrNo.substr(0, 17)))
        ...{
            document.myform.CardNo.focus();
            return false;
        }
        a = parseInt(StrNo.substr(0, 1)) * 7+parseInt(StrNo.substr(1, 1)) * 9 +parseInt(StrNo.substr(2, 1)) * 10;
        a = a + parseInt(StrNo.substr(3, 1)) * 5+parseInt(StrNo.substr(4, 1)) * 8+parseInt(StrNo.substr(5, 1)) * 4;
        a = a + parseInt(StrNo.substr(6, 1)) * 2+parseInt(StrNo.substr(7, 1)) * 1+parseInt(StrNo.substr(8, 1)) * 6;
        a = a + parseInt(StrNo.substr(9, 1)) * 3+parseInt(StrNo.substr(10, 1)) * 7+parseInt(StrNo.substr(11, 1)) * 9;
        a = a + parseInt(StrNo.substr(12, 1)) * 10+parseInt(StrNo.substr(13, 1)) * 5+parseInt(StrNo.substr(14, 1)) * 8;
        a = a + parseInt(StrNo.substr(15, 1)) * 4+parseInt(StrNo.substr(16, 1)) * 2;
        b = a % 11;

        if (b == 2)
        //最后一位为校验位  
        ...{
            c = StrNo.substr(17, 1).toUpperCase(); //转为大写X  
        }
        else
        ...{
            c = parseInt(StrNo.substr(17, 1));
        }

        switch (b)
        ...{
            case 0:
                if (c != 1)
                ...{
                    alert("身份证好号码校验位错:最后一位应该为:1");
                            document.myform.CardNo.focus();
                    return false;
                }
                break;
            case 1:
                if (c != 0)
                ...{
                    alert("身份证好号码校验位错:最后一位应该为:0");
                            document.myform.CardNo.focus();
                    return false;
                }
                break;
            case 2:
                if (c != "X")
                ...{
                    alert("身份证好号码校验位错:最后一位应该为:X");
                            document.myform.CardNo.focus();
                    return false;
                }
                break;
            case 3:
                if (c != 9)
                ...{
                    alert("身份证好号码校验位错:最后一位应该为:9");
                            document.myform.CardNo.focus();
                    return false;
                }
                break;
            case 4:
                if (c != 8)
                ...{
                    alert("身份证好号码校验位错:最后一位应该为:8");
                            document.myform.CardNo.focus();
                    return false;
                }
                break;
            case 5:
                if (c != 7)
                ...{
                    alert("身份证好号码校验位错:最后一位应该为:7");
                            document.myform.CardNo.focus();
                    return false;
                }
                break;
            case 6:
                if (c != 6)
                ...{
                    alert("身份证好号码校验位错:最后一位应该为:6");
                            document.myform.CardNo.focus();
                    return false;
                }
                break;
            case 7:
                if (c != 5)
                ...{
                    alert("身份证好号码校验位错:最后一位应该为:5");
                            document.myform.CardNo.focus();
                    return false;
                }
                break;
            case 8:
                if (c != 4)
                ...{
                    alert("身份证好号码校验位错:最后一位应该为:4");
                            document.myform.CardNo.focus();
                    return false;
                }
                break;
            case 9:
                if (c != 3)
                ...{
                    alert("身份证好号码校验位错:最后一位应该为:3");
                            document.myform.CardNo.focus();
                    return false;
                }
                break;
            case 10:
                if (c != 2)
                ...{
                    alert("身份证好号码校验位错:最后一位应该为:2");
                            document.myform.CardNo.focus();
                    return false
                }
        }
    }
    else if(StrNo.length==15)
    //15位身份证号  
    ...{
        if (!isInteger(StrNo))
        ...{
            alert("身份证号码错误,前15位不能含有英文字母!");
                    document.myform.CardNo.focus();
            return false;
        }
    }
    else
    ...{
        alert("输入的身份证号码必须为15位或者18位!");
        document.myform.CardNo.focus();
        return false;
        }

    switch (StrNo.length)
    ...{
        case 15:
            if (isValidDate("19" + StrNo.substr(6, 2), StrNo.substr(8, 2),StrNo.substr(10, 2)))
            ...{
                return true;
            }
            else
            ...{
                return false;
            }
        case 18:
            if (isValidDate(StrNo.substr(6, 4), StrNo.substr(10, 2),StrNo.substr(12, 2)))
            ...{
                return true;
            }
            else
            ...{
                return false;
            }
    }

}

function isValidDate(iY, iM, iD)
...{
    var a = new Date();
    var y = a.getFullYear();
//    var m = a.getMonth();
//    var d = a.getDate();
//    if (y != iY || m != iM || d != iD)
//    {
//        alert(y);
//        alert(m);
//        alert(d);
    if((iY<1900||iY>y)||(iM<=0||iM>12)||(iD>31||iD<=0))
    ...{
        window.alert('身份证号码内日期错误!');
                document.myform.CardNo.focus();
        return false;
    }
    return true;
}

function isInteger(str)
...{
    if (/[^d]+$/.test(str))
    ...{
        return false;
    }
    return true;
}


function IDUpdate(StrNo)
...{

    if (!isChinaIDCard(StrNo))
    ...{
        return false;
    }
    if (StrNo.length == 15)
    ...{
        var a, b, c; 
        StrNo = StrNo.substr(0, 6) + "19" + StrNo.substr(6, 9);
        a = parseInt(StrNo.substr(0, 1)) * 7+parseInt(StrNo.substr(1, 1)) * 9 +parseInt(StrNo.substr(2, 1)) * 10;
        a = a + parseInt(StrNo.substr(3, 1)) * 5+parseInt(StrNo.substr(4, 1)) * 8+parseInt(StrNo.substr(5, 1)) * 4;
        a = a + parseInt(StrNo.substr(6, 1)) * 2+parseInt(StrNo.substr(7, 1)) * 1+parseInt(StrNo.substr(8, 1)) * 6;
        a = a + parseInt(StrNo.substr(9, 1)) * 3+parseInt(StrNo.substr(10, 1)) * 7+parseInt(StrNo.substr(11, 1)) * 9;
        a = a + parseInt(StrNo.substr(12, 1)) * 10+parseInt(StrNo.substr(13, 1))* 5+parseInt(StrNo.substr(14, 1)) * 8;
        a = a + parseInt(StrNo.substr(15, 1)) * 4+parseInt(StrNo.substr(16, 1))* 2;
        b = a % 11;

        switch (b)
        ...{
            case 0:
                ...{
                    StrNo = StrNo + "1";
                }
                break;
            case 1:
                ...{
                    StrNo = StrNo + "0";
                }
                break;
            case 2:
                ...{
                    StrNo = StrNo + "X";
                }
                break;
            case 3:
                ...{
                    StrNo = StrNo + "9";
                }
                break;
            case 4:
                ...{
                    StrNo = StrNo + "8";
                }
                break;
            case 5:
                ...{
                    StrNo = StrNo + "7";
                }
                break;
            case 6:
                ...{
                    StrNo = StrNo + "6";
                }
                break;
            case 7:
                ...{
                    StrNo = StrNo + "5";
                }
                break;
            case 8:
                ...{
                    StrNo = StrNo + "4";
                }
                break;
            case 9:
                ...{
                    StrNo = StrNo + "3";
                }
                break;
            case 10:
                ...{
                    StrNo = StrNo + "3";
                }
        }
    }
    return StrNo;
}

网上搜集

//身份证号码校验,并从中拆分出出生年月日和性别
var yyyy;
var mm;
var dd;
var birthday;
var sex;

function getYear()...{
        return yyyy;
}
function getMonth()...{
        return mm;
}
function getDate()...{
        return dd;
}
function getBirthday()...{
        return birthday;
}
function getSex()...{
        return sex;
}
function getAge()...{
        var mm=getMonth();
        if (mm<10)
                mm=mm.substring(1,2);
        return Math.floor((parseInt(_getYear())*12+parseInt(_getMonth())-parseInt(getYear())*12-parseInt(mm))/12);
}
//判断是否大龄,男50,女40
function isBigAge()...{
        if (parseInt(getAge())>=40 && parseInt(getSex())==2)
            return "1";
        if (parseInt(getAge())>=50 && parseInt(getSex())==1)
            return "1";
        return "0";
}
//校验身份证号码
function CheckValue(idCard)...{
        var id=idCard;
        var id_length=id.length;

        if (id_length==0)...{
            alert("请输入身份证号码!");
            return false;
        }

        if (id_length!=15 && id_length!=18)...{
            alert("身份证号长度应为15位或18位!");
            return false;
        }

        if (id_length==15)...{
            yyyy="19"+id.substring(6,8);
            mm=id.substring(8,10);
            dd=id.substring(10,12);

            if (mm>12 || mm<=0)...{
                alert("输入身份证号,月份非法!");
                return false;
            }

            if (dd>31 || dd<=0)...{
                alert("输入身份证号,日期非法!");
                return false;
            }

            birthday=yyyy+ "-" +mm+ "-" +dd;

            if ("13579".indexOf(id.substring(14,15))!=-1)...{
                sex="1";
            }else...{
                sex="2";
            }
        }else if (id_length==18)...{
            if (id.indexOf("X") > 0 && id.indexOf("X")!=17 || id.indexOf("x")>0 && id.indexOf("x")!=17)...{
                alert("身份证中"X"输入位置不正确!");
                return false;
            }

            yyyy=id.substring(6,10);
            if (yyyy>2200 || yyyy<1900)...{
                alert("输入身份证号,年度非法!");
                return false;
            }

            mm=id.substring(10,12);
            if (mm>12 || mm<=0)...{
                alert("输入身份证号,月份非法!");
                return false;
            }

            dd=id.substring(12,14);
            if (dd>31 || dd<=0)...{
                alert("输入身份证号,日期非法!");
                return false;
            }

            if (id.charAt(17)=="x" || id.charAt(17)=="X")
            ...{
                if ("x"!=GetVerifyBit(id) && "X"!=GetVerifyBit(id))...{
                    alert("身份证校验错误,请检查最后一位!");
                    return false;
                }

            }else...{
                if (id.charAt(17)!=GetVerifyBit(id))...{
                    alert("身份证校验错误,请检查最后一位!");
                    return false;
                }
            }

            birthday=id.substring(6,10) + "-" + id.substring(10,12) + "-" + id.substring(12,14);
            if ("13579".indexOf(id.substring(16,17)) > -1)...{
                sex="1";
            }else...{
                sex="2";
            }
        }

        return true;
}
//15位转18位中,计算校验位即最后一位
function GetVerifyBit(id)...{
        var result;
        var nNum=eval(id.charAt(0)*7+id.charAt(1)*9+id.charAt(2)*10+id.charAt(3)*5+id.charAt(4)*8+id.charAt(5)*4+id.charAt(6)*2+id.charAt(7)*1+id.charAt(8)*6+id.charAt(9)*3+id.charAt(10)*7+id.charAt(11)*9+id.charAt(12)*10+id.charAt(13)*5+id.charAt(14)*8+id.charAt(15)*4+id.charAt(16)*2);
        nNum=nNum%11;
        switch (nNum) ...{
           case 0 :
              result="1";
              break;
           case 1 :
              result="0";
              break;
           case 2 :
              result="X";
              break;
           case 3 :
              result="9";
              break;
           case 4 :
              result="8";
              break;
           case 5 :
              result="7";
              break;
           case 6 :
              result="6";
              break;
           case 7 :
              result="5";
              break;
           case 8 :
              result="4";
              break;
           case 9 :
              result="3";
              break;
           case 10 :
              result="2";
              break;
        }
        //document.write(result);
        return result;
}
//15位转18位
function Get18(idCard)...{
if (CheckValue(idCard))...{
      var id = idCard;
      var id18=id;
      if (id.length==0)...{
       alert("请输入15位身份证号!");
       return false;
      }
      if (id.length==15)...{
       if (id.substring(6,8)>20)...{
        id18=id.substring(0,6)+"19"+id.substring(6,15);
       }else...{
        id18=id.substring(0,6)+"20"+id.substring(6,15);
       }

       id18=id18+GetVerifyBit(id18);
      }

      return id18;
}else...{
      return false;
}
}


function checkIdcard(idcard)...{
var Errors=new Array(
"验证通过!",
"身份证号码位数不对!",
"身份证号码出生日期超出范围或含有非法字符!",
"身份证号码校验错误!",
"身份证地区非法!"
);
var area=...{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 idcard,Y,JYM;
var S,M;
var idcard_array = new Array();
idcard_array = idcard.split("");
//地区检验
if(area[parseInt(idcard.substr(0,2))]==null) return Errors[4];
//身份号码位数及格式检验
switch(idcard.length)...{
case 15:
if ( (parseInt(idcard.substr(6,2))+1900) % 4 == 0 || ((parseInt(idcard.substr(6,2))+1900) % 100 == 0 && (parseInt(idcard.substr(6,2))+1900) % 4 == 0 ))...{
ereg=/^[1-9][0-9]...{5}[0 -9]...{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1 -9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]...{3}$/;//测试出生日期的合法性
} else ...{
ereg =/^[1-9][0-9]...{5}[0-9]...{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1]) |(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]...{3}$/; //测试出生日期的合法性
}
if(ereg.test(idcard)) return Errors[0];
else return Errors[2];
break;
case 18:
//18位身份号码检测
//出生日期的合法性检查
//闰年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))
//平年月日:((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))
if ( parseInt(idcard.substr(6,4)) % 4 == 0 || (parseInt(idcard.substr(6,4)) % 100 == 0 && parseInt(idcard.substr(6,4))%4 == 0 ))...{
ereg=/^[1-9][0-9]...{5}19[0-9]...{2} ((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1 -2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]...{3}[0-9Xx]$/;//闰年出生日期的合法性正则表达式
} else ...{
ereg =/^[1-9][0-9]...{5}19[0-9]...{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1]) |(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]...{3}[0- 9Xx]$/;//平年出生日期的合法性正则表达式
}
if(ereg.test(idcard))...{//测试出生日期的合法性
//计算校验位
S = (parseInt(idcard_array[0]) + parseInt(idcard_array[10])) * 7
+ (parseInt(idcard_array[1]) + parseInt(idcard_array[11])) * 9
+ (parseInt(idcard_array[2]) + parseInt(idcard_array[12])) * 10
+ (parseInt(idcard_array[3]) + parseInt(idcard_array[13])) * 5
+ (parseInt(idcard_array[4]) + parseInt(idcard_array[14])) * 8
+ (parseInt(idcard_array[5]) + parseInt(idcard_array[15])) * 4
+ (parseInt(idcard_array[6]) + parseInt(idcard_array[16])) * 2
+ parseInt(idcard_array[7]) * 1
+ parseInt(idcard_array[8]) * 6
+ parseInt(idcard_array[9]) * 3 ;
Y = S % 11;
M = "F";
JYM = "10X98765432";
M = JYM.substr(Y,1);//判断校验位
if(M == idcard_array[17]) return Errors[0]; //检测ID的校验位
else return Errors[3];
}
else return Errors[2];
break;
default:
return Errors[1];
break;
}

}
var sss = "000000000000000000"
document.write (checkIdcard(sss))
//-->


jsp

<%@page import="java.lang.String"%>
<%@page contentType="text/html" pageEncoding="gbk"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">


<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>身份证合法性验证</title>
        <style type="text/css">
            p{display: block;width:440px;}
            fieldset{width: 280px;}
            p span{width:50px;background: #ff0;
                   padding:2px;margin-right: 5px;
                 color:blue;}
        </style>
    </head>
    <body>
        <fieldset>
            <legend>
                身份证合法性验证
            </legend>
            <form action="">
           <%--
            verId方法中应按照如下算法进行对身份证进行验证:
            必须是18位,前17位必须是数字
             将身份证的前17位分别乘以7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 ,
            然后将得到的数字相加后除以11,将得到的余数按照以下转换表进行比对,
             如果左侧的余数对应的右侧数字和身份证号码的最后一位相同,则该身份证号码为真:
            0-->1 1-->0 2-->X 3-->9 4-->8 5-->7 6-->6 7-->5 8-->4 9-->3 10-->2
        --%>
          <%! //声明方法
               String[] ai=new String[17];//用户的身份证号码数组;
               int[] wi=new int[]{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};//17位数对应的加权因子,自左至右;
               int[] yi=new int[]{0,1,2,3,4,5,6,7,8,9,10};//余数;
               String[] vi={"1","0","X","9","8","7","6","5","4","3","2"};//余数对应的校验码;
          public static boolean IsNum(String str){//判断是否是数字
                for(int i=0;i<str.length();i++){
                  if(str.charAt(i)<'0' || str.charAt(i)>'9')
                        return false;
                          }
                       return true;
                 }// end IsNum
          int sum=0;int y;String v;
         public boolean verId(String idNo){//判断检验结果
               idNo=idNo.toLowerCase();
              if(idNo.length()==18){
                String n17=idNo.substring(0,17);//把obj第0位开始,长度为17的字符串赋给n17
                String endNum=idNo.substring(17,18);
                 if( IsNum(idNo) || ( IsNum(idNo.substring(0,17)) &&  endNum.equals("x") )){
                     //如果检查的是18为整数,或者前17位是整数未位是x;
                       String ay=getVer(n17);
                        if(ay.equals(endNum)){//如果得到的检验码与未位相符合刚返回true;否则都返回false;
                             return true;
                             }
                        else{
                            return false;
                          }
                    }//end of line65
                else{
                         return false;
                     }
              }//end of line62
               else{
                   return false;
                    }
           }
           public String getVer(String num17){//检验方法
                sum=0;
               if(!IsNum(num17) || num17.length()!=17){
                    return "不是17个数字";
                      }
               else{
                     for(int i=0;i<num17.length();i++){
                         sum+=Integer.parseInt(String.valueOf(num17.charAt(i)))*wi[i];
                         }
                      y=sum;v=vi[y];
                    return String.valueOf(v);
                       }
             }
        %>
            <%
             String idno="";
             if(request.getParameter("idnumber")!=null)
               idno=request.getParameter("idnumber");
            %>
                <p>请输入要验证的身份证号码:</p><input name="idnumber" value=<%=idno%> type="text"/>


                  <%
                  if(request.getParameter("five")==null){
                   double number=Math.random()*10;//这是产生一个比10小的数
                   if(number<9){
                       number=number+1;//这是让这个书比1大,防止转化成5位时会丢失位数
                       }
                   double n=number*10000;//让这个数变成5位的
                   int num=(int)n;//把随机数变成整数
                   session.setAttribute("rdom",num);//将随机数写入session
                   }
                   %>
                <p><span><%=session.getAttribute("rdom")%></span>输入左侧5位数字:</p>
                <input name="five" value=<%=request.getParameter("five")%> type="text"/>
                
                  <%
                  if(request.getParameter("five")!=null){
                      int random = (java.lang.Integer) session.getAttribute("rdom");
                      session.setAttribute("myput",request.getParameter("five"));
                      String myput=(String)session.getAttribute("myput");
                      int my=Integer.parseInt(myput);
                      if(my!=random){
                         out.println("<p style=\"color:red;\">验证码错误!</p>");
                             }
                      else{
                          if(verId(idno)){
                              out.println("<p style=\"color:green;font=weight:bold;\">该身份证合法!</p>");
                              }
                           else{
                                  out.println("<p style=\"color:red;font=weight:bold;\">该身份证不合法!</p>");
                                  }
                          }
                         }
                   %>
                <p><input type="submit" value="验证!" /></p>
            </form>
        </fieldset>
    </body>
</html>


身份证校验码(JS,PHP,JSP验证)            
身份证从15位升级至18位,我们知道有两位数据是在年份前面加上19,后面一位就是校验码。下面这个程序就是用来检验身份证升级至18位数的有效性的。分别用JavaScript,PHP,JSP做了效果。

源代码下载(含PHP,JSP,JS):http://jarryli.googlepages.com/checkIdentity.rar
演示地址:http://jarryli.googlepages.com/checkIdentity.html

校验方法: 
(1)十七位数字本体码加权求和公式 
S = Sum(Ai * Wi), i = 0, ... , 16 ,先对前17位数字的权求和 
Ai:表示第i位置上的身份证号码数字值 
Wi:表示第i位置上的加权因子 
Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2

(2)计算模 
Y = mod(S, 11)

(3)通过模得到对应的校验码 
Y: 0 1 2 3 4 5 6 7 8 9 10 
校验码: 1 0 X 9 8 7 6 5 4 3 2 
回答者: 55SS22ZZ88CC - 高级经理 七级 3-3 17:02

身份证校验码生成算法

根  
  分享到:  
0.2578s