java算法之身份证号码验证

来源:互联网 发布:mac上怎么卸载java 编辑:程序博客网 时间:2024/05/10 10:53

http://blog.csdn.net/jason0539/article/details/10248699

调用时直接

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

实现代码如下:

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


 

作者:jason0539

微博:http://weibo.com/2553717707

博客:http://blog.csdn.net/jason0539(转载请说明出处)



0 0
原创粉丝点击