• 校验身份证号码(转载)


      1 public class IDCardVerify {
      2 
      3     private String errorInfo;
      4 
      5     // wi =2(n-1)(mod 11)
      6     final int[] wi = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };
      7     // verify digit
      8     final int[] vi = { 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 };
      9     private int[] ai = new int[18];
     10     private static String[] _areaCode = { "11", "12", "13", "14", "15", "21",
     11             "22", "23", "31", "32", "33", "34", "35", "36", "37", "41", "42",
     12             "43", "44", "45", "46", "50", "51", "52", "53", "54", "61", "62",
     13             "63", "64", "65", "71", "81", "82", "91" };
     14     private static HashMap<String, Integer> dateMap;
     15     private static HashMap<String, String> areaCodeMap;
     16     static {
     17         dateMap = new HashMap<String, Integer>();
     18         dateMap.put("01", 31);
     19         dateMap.put("02", null);
     20         dateMap.put("03", 31);
     21         dateMap.put("04", 30);
     22         dateMap.put("05", 31);
     23         dateMap.put("06", 30);
     24         dateMap.put("07", 31);
     25         dateMap.put("08", 31);
     26         dateMap.put("09", 30);
     27         dateMap.put("10", 31);
     28         dateMap.put("11", 30);
     29         dateMap.put("12", 31);
     30         areaCodeMap = new HashMap<String, String>();
     31         for (String code : _areaCode) {
     32             areaCodeMap.put(code, null);
     33         }
     34     }
     35 
     36     // 验证身份证位数,15位和18位身份证
     37     public boolean verifyLength(String code) {
     38         int length = code.length();
     39         if (length == 15 || length == 18) {
     40             return true;
     41         } else {
     42             errorInfo = "错误:输入的身份证号不是15位和18位的";
     43             return false;
     44         }
     45     }
     46 
     47     // 判断地区码
     48     public boolean verifyAreaCode(String code) {
     49         String areaCode = code.substring(0, 2);
     50         // Element child= _areaCodeElement.getChild("_"+areaCode);
     51         if (areaCodeMap.containsKey(areaCode)) {
     52             return true;
     53         } else {
     54             errorInfo = "错误:输入的身份证号的地区码(1-2位)[" + areaCode
     55                     + "]不符合中国行政区划分代码规定(GB/T2260-1999)";
     56             Log.e("---------", errorInfo);
     57             return false;
     58         }
     59     }
     60 
     61     // 判断月份和日期
     62     public boolean verifyBirthdayCode(String code) {
     63         // 验证月份
     64         String month = code.substring(10, 12);
     65         boolean isEighteenCode = (18 == code.length());
     66         if (!dateMap.containsKey(month)) {
     67             errorInfo = "错误:输入的身份证号"
     68                     + (isEighteenCode ? "(11-12位)" : "(9-10位)") + "不存在["
     69                     + month + "]月份,不符合要求(GB/T7408)";
     70             return false;
     71         }
     72         // 验证日期
     73         String dayCode = code.substring(12, 14);
     74         Integer day = dateMap.get(month);
     75         String yearCode = code.substring(6, 10);
     76         Integer year = Integer.valueOf(yearCode);
     77         String currentTime = StringUtil.formateDateToDay(System
     78                 .currentTimeMillis());
     79         Integer currentYear = Integer.valueOf(currentTime.substring(0, 4));
     80         int age = currentYear - year;
     81         // Log.e("----------", currentTime+";"+currentYear+";"+age+"");
     82         if (age <= 16 || age >= 115) {
     83             return false;
     84         }
     85         // 非2月的情况
     86         if (day != null) {
     87             if (Integer.valueOf(dayCode) > day || Integer.valueOf(dayCode) < 1) {
     88                 errorInfo = "错误:输入的身份证号"
     89                         + (isEighteenCode ? "(13-14位)" : "(11-13位)") + "["
     90                         + dayCode + "]号不符合小月1-30天大月1-31天的规定(GB/T7408)";
     91                 return false;
     92             }
     93         }
     94         // 2月的情况
     95         else {
     96             // 闰月的情况
     97             if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
     98                 if (Integer.valueOf(dayCode) > 29
     99                         || Integer.valueOf(dayCode) < 1) {
    100                     errorInfo = "错误:输入的身份证号"
    101                             + (isEighteenCode ? "(13-14位)" : "(11-13位)") + "["
    102                             + dayCode + "]号在" + year
    103                             + "闰年的情况下未符合1-29号的规定(GB/T7408)";
    104                     return false;
    105                 }
    106             }
    107             // 非闰月的情况
    108             else {
    109                 if (Integer.valueOf(dayCode) > 28
    110                         || Integer.valueOf(dayCode) < 1) {
    111                     errorInfo = "错误:输入的身份证号"
    112                             + (isEighteenCode ? "(13-14位)" : "(11-13位)") + "["
    113                             + dayCode + "]号在" + year
    114                             + "平年的情况下未符合1-28号的规定(GB/T7408)";
    115                     return false;
    116                 }
    117             }
    118         }
    119         return true;
    120     }
    121 
    122     // 验证身份除了最后位其他的是否包含字母
    123     public boolean containsAllNumber(String code) {
    124         String str = "";
    125         if (code.length() == 15) {
    126             str = code.substring(0, 15);
    127         } else if (code.length() == 18) {
    128             str = code.substring(0, 17);
    129         }
    130         char[] ch = str.toCharArray();
    131         for (int i = 0; i < ch.length; i++) {
    132             if (!(ch[i] >= '0' && ch[i] <= '9')) {
    133                 errorInfo = "错误:输入的身份证号第" + (i + 1) + "位包含字母";
    134                 return false;
    135             }
    136         }
    137         return true;
    138     }
    139 
    140     public String getCodeError() {
    141         return errorInfo;
    142     }
    143 
    144     // 验证身份证
    145     public boolean verify(String idcard) {
    146         errorInfo = "";
    147         // 验证身份证位数,15位和18位身份证
    148         if (!verifyLength(idcard)) {
    149             return false;
    150         }
    151         // 验证身份除了最后位其他的是否包含字母
    152         if (!containsAllNumber(idcard)) {
    153             return false;
    154         }
    155 
    156         // 如果是15位的就转成18位的身份证
    157         String eifhteencard = "";
    158         if (idcard.length() == 15) {
    159             eifhteencard = uptoeighteen(idcard);
    160         } else {
    161             eifhteencard = idcard;
    162         }
    163         // 验证身份证的地区码
    164         if (!verifyAreaCode(eifhteencard)) {
    165             return false;
    166         }
    167         // 判断月份和日期
    168         if (!verifyBirthdayCode(eifhteencard)) {
    169             return false;
    170         }
    171         // 验证18位校验码,校验码采用ISO 7064:1983,MOD 11-2 校验码系统
    172         if (!verifyMOD(eifhteencard)) {
    173             return false;
    174         }
    175         return true;
    176     }
    177 
    178     // 验证18位校验码,校验码采用ISO 7064:1983,MOD 11-2 校验码系统
    179     public boolean verifyMOD(String code) {
    180         String verify = code.substring(17, 18);
    181         if ("x".equals(verify)) {
    182             code = code.replaceAll("x", "X");
    183             verify = "X";
    184         }
    185         String verifyIndex = getVerify(code);
    186         if (verify.equals(verifyIndex)) {
    187             return true;
    188         }
    189         // int x=17;
    190         // if(code.length()==15){
    191         // x=14;
    192         // }
    193         errorInfo = "错误:输入的身份证号最末尾的数字验证码错误";
    194         return false;
    195     }
    196 
    197     // 获得校验位
    198     public String getVerify(String eightcardid) {
    199         int remaining = 0;
    200 
    201         if (eightcardid.length() == 18) {
    202             eightcardid = eightcardid.substring(0, 17);
    203         }
    204 
    205         if (eightcardid.length() == 17) {
    206             int sum = 0;
    207             for (int i = 0; i < 17; i++) {
    208                 String k = eightcardid.substring(i, i + 1);
    209                 ai[i] = Integer.parseInt(k);
    210             }
    211 
    212             for (int i = 0; i < 17; i++) {
    213                 sum = sum + wi[i] * ai[i];
    214             }
    215             remaining = sum % 11;
    216         }
    217 
    218         return remaining == 2 ? "X" : String.valueOf(vi[remaining]);
    219     }
    220 
    221     // 15位转18位身份证
    222     public String uptoeighteen(String fifteencardid) {
    223         String eightcardid = fifteencardid.substring(0, 6);
    224         eightcardid = eightcardid + "19";
    225         eightcardid = eightcardid + fifteencardid.substring(6, 15);
    226         eightcardid = eightcardid + getVerify(eightcardid);
    227         return eightcardid;
    228     }
    229 }
    View Code
  • 相关阅读:
    poj 3613(经过N条边的最短路)
    poj 3328(多起点多终点的最短路)
    poj 3311(floyd+状态压缩)
    新CCIE笔记-IP网络基础
    新CCIE笔记-IP网络基础
    算法之【冒泡排序法】
    算法之【冒泡排序法】
    算法之【冒泡排序法】
    算法之【辗转相除法】
    算法之【辗转相除法】
  • 原文地址:https://www.cnblogs.com/ylt-niuniu/p/4252080.html
Copyright © 2020-2023  润新知