java算法之身份证号码验证

调用时直接

new IDCard().verify(身份证id);就可以了

实现代码如下:

[java][/java] view plaincopyprint?

  1. public class IDCard {
  2.       private String _codeError;
  3.       //wi =2(n-1)(mod 11)
  4.       final int[] wi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};
  5.       // verify digit
  6.       final int[] vi = {1, 0, ‘X’, 9, 8, 7, 6, 5, 4, 3, 2};
  7.       private int[] ai = new int[18];
  8.       private static String[] _areaCode={“11″,”12″,”13″,”14″,”15″,”21″,”22”
  9.           ,”23″,”31″,”32″,”33″,”34″,”35″,”36″,”37″,”41″,”42″,”43″,”44″
  10.           ,”45″,”46″,”50″,”51″,”52″,”53″,”54″,”61″,”62″,”63″,”64″,”65″,”71″,”81″,”82″,”91″};
  11.       private static HashMap<String,Integer> dateMap;
  12.       private static HashMap<String,String> areaCodeMap;
  13.       static{
  14.             dateMap=new HashMap<String,Integer>();
  15.             dateMap.put(“01”,31);
  16.             dateMap.put(“02”,null);
  17.             dateMap.put(“03”,31);
  18.             dateMap.put(“04”,30);
  19.             dateMap.put(“05”,31);
  20.             dateMap.put(“06”,30);
  21.             dateMap.put(“07”,31);
  22.             dateMap.put(“08”,31);
  23.             dateMap.put(“09”,30);
  24.             dateMap.put(“10”,31);
  25.             dateMap.put(“11”,30);
  26.             dateMap.put(“12”,31);
  27.             areaCodeMap=new HashMap<String,String>();
  28.             for(String code:_areaCode){
  29.                   areaCodeMap.put(code,null);
  30.             }
  31.       }
  32.       //验证身份证位数,15位和18位身份证
  33.       public boolean verifyLength(String code){
  34.             int length=code.length();
  35.             if(length==15 || length==18){
  36.                   return true;
  37.             }else{
  38.                   _codeError=”错误:输入的身份证号不是15位和18位的”;
  39.                   return false;
  40.             }
  41.       }
  42.       //判断地区码
  43.       public boolean verifyAreaCode(String code){
  44.             String areaCode=code.substring(0,2);
  45. //            Element child=  _areaCodeElement.getChild(“_”+areaCode);
  46.             if(areaCodeMap.containsKey(areaCode)){
  47.                   return true;
  48.             }else{
  49.                   _codeError=”错误:输入的身份证号的地区码(1-2位)[“+areaCode+”]不符合中国行政区划分代码规定(GB/T2260-1999)”;
  50.                   return false;
  51.             }
  52.       }
  53.       //判断月份和日期
  54.       public boolean verifyBirthdayCode(String code){
  55.             //验证月份
  56.             String month=code.substring(10,12);
  57.             boolean isEighteenCode=(18==code.length());
  58.             if(!dateMap.containsKey(month)){
  59.                   _codeError=”错误:输入的身份证号”+(isEighteenCode?”(11-12位)”:”(9-10位)”)+”不存在[“+month+”]月份,不符合要求(GB/T7408)”;
  60.                   return false;
  61.             }
  62.             //验证日期
  63.             String dayCode=code.substring(12,14);
  64.             Integer day=dateMap.get(month);
  65.             String yearCode=code.substring(6,10);
  66.             Integer year=Integer.valueOf(yearCode);
  67.             //非2月的情况
  68.             if(day!=null){
  69.                   if(Integer.valueOf(dayCode)>day || Integer.valueOf(dayCode)<1){
  70.                         _codeError=”错误:输入的身份证号”+(isEighteenCode?”(13-14位)”:”(11-13位)”)+”[“+dayCode+”]号不符合小月1-30天大月1-31天的规定(GB/T7408)”;
  71.                         return false;
  72.                   }
  73.             }
  74.             //2月的情况
  75.             else{
  76.                   //闰月的情况
  77.                   if((year%4==0&&year%100!=0)||(year%400==0)){
  78.                         if(Integer.valueOf(dayCode)>29 || Integer.valueOf(dayCode)<1){
  79.                               _codeError=”错误:输入的身份证号”+(isEighteenCode?”(13-14位)”:”(11-13位)”)+”[“+dayCode+”]号在”+year+”闰年的情况下未符合1-29号的规定(GB/T7408)”;
  80.                               return false;
  81.                         }
  82.                   }
  83.                   //非闰月的情况
  84.                   else{
  85.                         if (Integer.valueOf(dayCode) > 28 || Integer.valueOf(dayCode) < 1) {
  86.                               _codeError=”错误:输入的身份证号”+(isEighteenCode?”(13-14位)”:”(11-13位)”)+”[“+dayCode+”]号在”+year+”平年的情况下未符合1-28号的规定(GB/T7408)”;
  87.                               return false;
  88.                         }
  89.                   }
  90.             }
  91.             return true;
  92.       }
  93.       //验证身份除了最后位其他的是否包含字母
  94.       public boolean containsAllNumber(String code) {
  95.             String str=””;
  96.             if(code.length()==15){
  97.                   str=code.substring(0,15);
  98.             }else if(code.length()==18){
  99.                   str=code.substring(0,17);
  100.             }
  101.             char[] ch = str.toCharArray();
  102.             for (int i = 0; i < ch.length; i++) {
  103.                   if (! (ch[i] >= ‘0’ && ch[i] <= ‘9’)) {
  104.                         _codeError=”错误:输入的身份证号第”+(i+1)+”位包含字母”;
  105.                         return false;
  106.                   }
  107.             }
  108.             return true;
  109.       }
  110.       public String getCodeError(){
  111.             return _codeError;
  112.       }
  113.       //验证身份证
  114.       public boolean verify(String idcard) {
  115.             _codeError=””;
  116.             //验证身份证位数,15位和18位身份证
  117.             if(!verifyLength(idcard)){
  118.                 return false;
  119.             }
  120.             //验证身份除了最后位其他的是否包含字母
  121.             if(!containsAllNumber(idcard)){
  122.                   return false;
  123.             }
  124.             //如果是15位的就转成18位的身份证
  125.             String eifhteencard=””;
  126.             if (idcard.length() == 15) {
  127.                   eifhteencard = uptoeighteen(idcard);
  128.             }else{
  129.                   eifhteencard=idcard;
  130.             }
  131.             //验证身份证的地区码
  132.             if(!verifyAreaCode(eifhteencard)){
  133.                   return false;
  134.             }
  135.             //判断月份和日期
  136.             if(!verifyBirthdayCode(eifhteencard)){
  137.                   return false;
  138.             }
  139.             //验证18位校验码,校验码采用ISO 7064:1983,MOD 11-2 校验码系统
  140.             if(!verifyMOD(eifhteencard)){
  141.                   return false;
  142.             }
  143.             return true;
  144.       }
  145.       //验证18位校验码,校验码采用ISO 7064:1983,MOD 11-2 校验码系统
  146.       public boolean verifyMOD(String code){
  147.             String verify = code.substring(17, 18);
  148.             if(“x”.equals(verify)){
  149.                   code=code.replaceAll(“x”,”X”);
  150.                   verify=”X”;
  151.             }
  152.             String verifyIndex=getVerify(code);
  153.             if (verify.equals(verifyIndex)) {
  154.                   return true;
  155.             }
  156. //            int x=17;
  157. //            if(code.length()==15){
  158. //                  x=14;
  159. //            }
  160.             _codeError=”错误:输入的身份证号最末尾的数字验证码错误”;
  161.             return false;
  162.       }
  163.       //获得校验位
  164.       public String getVerify(String eightcardid) {
  165.             int remaining = 0;
  166.             if (eightcardid.length() == 18) {
  167.                   eightcardid = eightcardid.substring(0, 17);
  168.             }
  169.             if (eightcardid.length() == 17) {
  170.                   int sum = 0;
  171.                   for (int i = 0; i < 17; i++) {
  172.                         String k = eightcardid.substring(i, i + 1);
  173.                         ai[i] = Integer.parseInt(k);
  174.                   }
  175.                   for (int i = 0; i < 17; i++) {
  176.                         sum = sum + wi[i] * ai[i];
  177.                   }
  178.                   remaining = sum % 11;
  179.             }
  180.             return remaining == 2 ? “X” : String.valueOf(vi[remaining]);
  181.       }
  182.       //15位转18位身份证
  183.       public String uptoeighteen(String fifteencardid) {
  184.             String eightcardid = fifteencardid.substring(0, 6);
  185.             eightcardid = eightcardid + “19”;
  186.             eightcardid = eightcardid + fifteencardid.substring(6, 15);
  187.             eightcardid = eightcardid + getVerify(eightcardid);
  188.             return eightcardid;
  189.       }

标签