java校验身份证号码

  1. /** 
  2.      * 18位身份证校验,粗略的校验 
  3.      * @author lyl 
  4.      * @param idCard 
  5.      * @return 
  6.      */  
  7.     public static boolean is18ByteIdCard(String idCard){  
  8.         Pattern pattern1 = Pattern.compile("^(\\d{6})(19|20)(\\d{2})(1[0-2]|0[1-9])(0[1-9]|[1-2][0-9]|3[0-1])(\\d{3})(\\d|X|x)?$"); //粗略的校验  
  9.         Matcher matcher = pattern1.matcher(idCard);  
  10.         if(matcher.matches()){  
  11.             return true;  
  12.         }  
  13.         return false;  
  14.     }  
  15.     /** 
  16.      * 18位身份证校验,比较严格校验 
  17.      * @author lyl 
  18.      * @param idCard 
  19.      * @return 
  20.      */  
  21.     public static boolean is18ByteIdCardComplex(String idCard){  
  22.         Pattern pattern1 = Pattern.compile("^(\\d{6})(19|20)(\\d{2})(1[0-2]|0[1-9])(0[1-9]|[1-2][0-9]|3[0-1])(\\d{3})(\\d|X|x)?$");   
  23.         Matcher matcher = pattern1.matcher(idCard);  
  24.         int[] prefix = new int[]{7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};  
  25.         int[] suffix = new int[]{ 1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2 };  
  26.         if(matcher.matches()){  
  27.             Map<String, String> cityMap = initCityMap();  
  28.             if(cityMap.get(idCard.substring(0,2)) == null ){  
  29.                 return false;  
  30.             }  
  31.             int idCardWiSum=0; //用来保存前17位各自乖以加权因子后的总和  
  32.             for(int i=0;i<17;i++){  
  33.                 idCardWiSum+=Integer.valueOf(idCard.substring(i,i+1))*prefix[i];  
  34.             }  
  35.               
  36.             int idCardMod=idCardWiSum%11;//计算出校验码所在数组的位置  
  37.             String idCardLast=idCard.substring(17);//得到最后一位身份证号码  
  38.               
  39.             //如果等于2,则说明校验码是10,身份证号码最后一位应该是X  
  40.             if(idCardMod==2){  
  41.                 if(idCardLast.equalsIgnoreCase("x")){  
  42.                     return true;  
  43.                 }else{  
  44.                     return false;  
  45.                 }  
  46.             }else{  
  47.                 //用计算出的验证码与最后一位身份证号码匹配,如果一致,说明通过,否则是无效的身份证号码  
  48.                 if(idCardLast.equals(suffix[idCardMod]+"")){  
  49.                     return true;  
  50.                 }else{  
  51.                     return false;  
  52.                 }  
  53.            }  
  54.         }  
  55.         return false;  
  56.     }  
  57.       
  58.     private static Map<String, String> initCityMap(){  
  59.         Map<String, String> cityMap = new HashMap<String, String>();  
  60.             cityMap.put("11", "北京");  
  61.             cityMap.put("12", "天津");  
  62.             cityMap.put("13", "河北");  
  63.             cityMap.put("14", "山西");  
  64.             cityMap.put("15", "内蒙古");  
  65.               
  66.             cityMap.put("21", "辽宁");  
  67.             cityMap.put("22", "吉林");  
  68.             cityMap.put("23", "黑龙江");  
  69.               
  70.             cityMap.put("31", "上海");  
  71.             cityMap.put("32", "江苏");  
  72.             cityMap.put("33", "浙江");  
  73.             cityMap.put("34", "安徽");  
  74.             cityMap.put("35", "福建");  
  75.             cityMap.put("36", "江西");  
  76.             cityMap.put("37", "山东");  
  77.               
  78.             cityMap.put("41", "河南");  
  79.             cityMap.put("42", "湖北");  
  80.             cityMap.put("43", "湖南");  
  81.             cityMap.put("44", "广东");  
  82.             cityMap.put("45", "广西");  
  83.             cityMap.put("46", "海南");  
  84.               
  85.             cityMap.put("50", "重庆");  
  86.             cityMap.put("51", "四川");  
  87.             cityMap.put("52", "贵州");  
  88.             cityMap.put("53", "云南");  
  89.             cityMap.put("54", "西藏");  
  90.               
  91.             cityMap.put("61", "陕西");  
  92.             cityMap.put("62", "甘肃");  
  93.             cityMap.put("63", "青海");  
  94.             cityMap.put("64", "宁夏");  
  95.             cityMap.put("65", "新疆");  
  96.               
  97. //          cityMap.put("71", "台湾");  
  98. //          cityMap.put("81", "香港");  
  99. //          cityMap.put("82", "澳门");  
  100. //          cityMap.put("91", "国外");  
  101. //          System.out.println(cityMap.keySet().size());  
  102.             return cityMap;  
  103.         } 
posted @ 2017-11-02 17:52  kim_liu  阅读(1889)  评论(0编辑  收藏  举报