分享

身份证号码验证(15位和18位且最后一位是X时不区分大小写)

 风_宇星 2015-02-13
Java代码  收藏代码
  1. import java.text.ParseException;  
  2. import java.text.SimpleDateFormat;  
  3. import java.util.Calendar;  
  4. import java.util.GregorianCalendar;  
  5. import java.util.Hashtable;  
  6. import java.util.regex.Matcher;  
  7. import java.util.regex.Pattern;  
  8.   
  9. public class IdCardUtils {  
  10.   //身份证号码验证:start  
  11.       
  12.     /**   
  13.         * 功能:身份证的有效验证   
  14.         * @param IDStr 身份证号   
  15.         * @return 有效:返回"" 无效:返回String信息   
  16.         * @throws ParseException   
  17.         */    
  18.     @SuppressWarnings("rawtypes")  
  19.     public static boolean IDCardValidate(String IDStr) throws ParseException {     
  20.            String[] ValCodeArr = { "1", "0", "x", "9", "8", "7", "6", "5", "4",     
  21.                    "3", "2" };     
  22.            String[] Wi = { "7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7",     
  23.                    "9", "10", "5", "8", "4", "2" };     
  24.            String Ai = "";   
  25.            String Ai1 = "";  
  26.            String Ai2 = "";  
  27.            // ================ 号码的长度 15位或18位 ================     
  28.            if (IDStr.length() != 15 && IDStr.length() != 18) {      
  29.                return false;     
  30.            }     
  31.            // =======================(end)========================     
  32.        
  33.            // ================ 数字 除最后以为都为数字 ================     
  34.            if (IDStr.length() == 18) {     
  35.                Ai = IDStr.substring(0, 17);     
  36.            } else if (IDStr.length() == 15) {     
  37.                Ai = IDStr.substring(0, 6) + "19" + IDStr.substring(6, 15);     
  38.            }     
  39.            if (isNumeric(Ai) == false) {      
  40.                return false;     
  41.            }     
  42.            // =======================(end)========================     
  43.        
  44.            // ================ 出生年月是否有效 ================     
  45.            String strYear = Ai.substring(6, 10);// 年份     
  46.            String strMonth = Ai.substring(10, 12);// 月份     
  47.            String strDay = Ai.substring(12, 14);// 月份     
  48.            if (isDataFormat(strYear + "-" + strMonth + "-" + strDay) == false) {        
  49.                return false;     
  50.            }     
  51.            GregorianCalendar gc = new GregorianCalendar();     
  52.            SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");     
  53.            if ((gc.get(Calendar.YEAR) - Integer.parseInt(strYear)) > 150    
  54.                    || (gc.getTime().getTime() - s.parse(     
  55.                            strYear + "-" + strMonth + "-" + strDay).getTime()) < 0) {       
  56.                return false;     
  57.            }     
  58.            if (Integer.parseInt(strMonth) > 12 || Integer.parseInt(strMonth) == 0) {        
  59.                return false;     
  60.            }     
  61.            if (Integer.parseInt(strDay) > 31 || Integer.parseInt(strDay) == 0) {       
  62.                return false;     
  63.            }     
  64.            // =====================(end)=====================     
  65.        
  66.            // ================ 地区码时候有效 ================     
  67.            Hashtable h = GetAreaCode();     
  68.            if (h.get(Ai.substring(0, 2)) == null) {        
  69.                return false;     
  70.            }     
  71.            // ==============================================     
  72.        
  73.            // ================ 判断最后一位的值 ================     
  74.            int TotalmulAiWi = 0;     
  75.            for (int i = 0; i < 17; i++) {     
  76.                TotalmulAiWi = TotalmulAiWi     
  77.                        + Integer.parseInt(String.valueOf(Ai.charAt(i)))     
  78.                        * Integer.parseInt(Wi[i]);     
  79.            }     
  80.            int modValue = TotalmulAiWi % 11;     
  81.            String strVerifyCode = ValCodeArr[modValue];     
  82.            Ai1 = Ai + strVerifyCode.toUpperCase();     
  83.            Ai2 = Ai + strVerifyCode.toLowerCase();  
  84.            if (IDStr.length() == 18) {     
  85.               if (Ai1.equals(IDStr) == false && Ai2.equals(IDStr) == false) {      
  86.                   return false;     
  87.               }     
  88.            } else {     
  89.                 return true;     
  90.             }     
  91.             // =====================(end)=====================     
  92.             return true;     
  93.         }  
  94.          
  95.        /**   
  96.          * 功能:判断字符串是否为数字   
  97.          * @param str   
  98.          * @return   
  99.          */    
  100.         private static boolean isNumeric(String str) {     
  101.             Pattern pattern = Pattern.compile("[0-9]*");     
  102.             Matcher isNum = pattern.matcher(str);     
  103.             if (isNum.matches()) {     
  104.                 return true;     
  105.             } else {     
  106.                 return false;     
  107.             }     
  108.         }  
  109.           
  110.         /**   
  111.          * 功能:设置地区编码   
  112.          * @return Hashtable 对象   
  113.          */    
  114.         @SuppressWarnings({ "rawtypes", "unchecked" })  
  115.         private static Hashtable GetAreaCode() {     
  116.             Hashtable hashtable = new Hashtable();     
  117.             hashtable.put("11", "北京");     
  118.             hashtable.put("12", "天津");     
  119.             hashtable.put("13", "河北");     
  120.             hashtable.put("14", "山西");     
  121.             hashtable.put("15", "内蒙古");     
  122.             hashtable.put("21", "辽宁");     
  123.             hashtable.put("22", "吉林");     
  124.             hashtable.put("23", "黑龙江");     
  125.             hashtable.put("31", "上海");     
  126.             hashtable.put("32", "江苏");     
  127.             hashtable.put("33", "浙江");     
  128.             hashtable.put("34", "安徽");     
  129.             hashtable.put("35", "福建");     
  130.             hashtable.put("36", "江西");     
  131.             hashtable.put("37", "山东");     
  132.             hashtable.put("41", "河南");     
  133.             hashtable.put("42", "湖北");     
  134.             hashtable.put("43", "湖南");     
  135.             hashtable.put("44", "广东");     
  136.             hashtable.put("45", "广西");     
  137.             hashtable.put("46", "海南");     
  138.             hashtable.put("50", "重庆");     
  139.             hashtable.put("51", "四川");     
  140.             hashtable.put("52", "贵州");     
  141.             hashtable.put("53", "云南");     
  142.             hashtable.put("54", "西藏");     
  143.             hashtable.put("61", "陕西");     
  144.             hashtable.put("62", "甘肃");     
  145.             hashtable.put("63", "青海");     
  146.             hashtable.put("64", "宁夏");     
  147.             hashtable.put("65", "新疆");     
  148.             hashtable.put("71", "台湾");     
  149.             hashtable.put("81", "香港");     
  150.             hashtable.put("82", "澳门");     
  151.             hashtable.put("91", "国外");     
  152.             return hashtable;     
  153.         }    
  154.   
  155.    /**验证日期字符串是否是YYYY-MM-DD格式 
  156.      * @param str 
  157.      * @return 
  158.      */  
  159.     public static boolean isDataFormat(String str){  
  160.      boolean flag=false;  
  161.      //String regxStr="[1-9][0-9]{3}-[0-1][0-2]-((0[1-9])|([12][0-9])|(3[01]))";  
  162.      String regxStr="^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1-2][0-3]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$";  
  163.      Pattern pattern1=Pattern.compile(regxStr);  
  164.      Matcher isNo=pattern1.matcher(str);  
  165.      if(isNo.matches()){  
  166.       flag=true;  
  167.      }  
  168.      return flag;  
  169.     }  
  170.   
  171.     //身份证号码验证:end   
  172. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多