身份证格式验证

来源:互联网 发布:贪玩蓝月附灵数据 编辑:程序博客网 时间:2024/04/29 23:17
[java] view plain copy
  1. package com.sinosoft.utils;  
  2.   
  3. import java.text.ParseException;  
  4. import java.text.SimpleDateFormat;  
  5. import java.util.Calendar;  
  6. import java.util.GregorianCalendar;  
  7. import java.util.Hashtable;  
  8. import java.util.regex.Matcher;  
  9. import java.util.regex.Pattern;  
  10.   
  11. public class IDCardValidate {  
  12.   
  13.     //身份证号码验证:start  
  14.        /**  
  15.          * 功能:身份证的有效验证  
  16.          * @param IDStr 身份证号  
  17.          * @return 有效:返回"" 无效:返回String信息  
  18.          * @throws ParseException  
  19.          */   
  20.         public static String IDCardValidate(String IDStr) throws ParseException {    
  21.             String errorInfo = "";// 记录错误信息    
  22.             String[] ValCodeArr = { "1""0""x""9""8""7""6""5""4",    
  23.                     "3""2" };    
  24.             String[] Wi = { "7""9""10""5""8""4""2""1""6""3""7",    
  25.                     "9""10""5""8""4""2" };    
  26.             String Ai = "";    
  27.             // ================ 号码的长度 15位或18位 ================    
  28.             if (IDStr.length() != 15 && IDStr.length() != 18) {    
  29.                 errorInfo = "身份证号码长度应该为15位或18位。";    
  30.                 return errorInfo;    
  31.             }    
  32.             // =======================(end)========================    
  33.        
  34.             // ================ 数字 除最后以为都为数字 ================    
  35.             if (IDStr.length() == 18) {    
  36.                 Ai = IDStr.substring(017);    
  37.             } else if (IDStr.length() == 15) {    
  38.                 Ai = IDStr.substring(06) + "19" + IDStr.substring(615);    
  39.             }    
  40.             if (isNumeric(Ai) == false) {    
  41.                 errorInfo = "身份证15位号码都应为数字 ; 18位号码除最后一位外,都应为数字。";    
  42.                 return errorInfo;    
  43.             }    
  44.             // =======================(end)========================    
  45.        
  46.             // ================ 出生年月是否有效 ================    
  47.             String strYear = Ai.substring(610);// 年份    
  48.             String strMonth = Ai.substring(1012);// 月份    
  49.             String strDay = Ai.substring(1214);// 月份    
  50.             if (isDataFormat(strYear + "-" + strMonth + "-" + strDay) == false) {    
  51.                 errorInfo = "身份证生日无效。";    
  52.                 return errorInfo;    
  53.             }    
  54.             GregorianCalendar gc = new GregorianCalendar();    
  55.             SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");    
  56.             if ((gc.get(Calendar.YEAR) - Integer.parseInt(strYear)) > 150   
  57.                     || (gc.getTime().getTime() - s.parse(    
  58.                             strYear + "-" + strMonth + "-" + strDay).getTime()) < 0) {    
  59.                 errorInfo = "身份证生日不在有效范围。";    
  60.                 return errorInfo;    
  61.             }    
  62.             if (Integer.parseInt(strMonth) > 12 || Integer.parseInt(strMonth) == 0) {    
  63.                 errorInfo = "身份证月份无效";    
  64.                 return errorInfo;    
  65.             }    
  66.             if (Integer.parseInt(strDay) > 31 || Integer.parseInt(strDay) == 0) {    
  67.                 errorInfo = "身份证日期无效";    
  68.                 return errorInfo;    
  69.             }    
  70.             // =====================(end)=====================    
  71.        
  72.        
  73.             // ==============================================    
  74.        
  75.             // ================ 判断最后一位的值 ================    
  76.             int TotalmulAiWi = 0;    
  77.             for (int i = 0; i < 17; i++) {    
  78.                 TotalmulAiWi = TotalmulAiWi    
  79.                         + Integer.parseInt(String.valueOf(Ai.charAt(i)))    
  80.                         * Integer.parseInt(Wi[i]);    
  81.             }    
  82.             int modValue = TotalmulAiWi % 11;    
  83.             String strVerifyCode = ValCodeArr[modValue];    
  84.             Ai = Ai + strVerifyCode;    
  85.        
  86.             if (IDStr.length() == 18) {    
  87.                  if (Ai.equals(IDStr) == false) {    
  88.                      errorInfo = "身份证无效,不是合法的身份证号码";    
  89.                      return errorInfo;    
  90.                  }    
  91.              } else {    
  92.                  return "";    
  93.              }    
  94.              // =====================(end)=====================    
  95.              return "";    
  96.          }  
  97.          
  98.         /**  
  99.           * 功能:判断字符串是否为数字  
  100.           * @param str  
  101.           * @return  
  102.           */   
  103.          private static boolean isNumeric(String str) {    
  104.              Pattern pattern = Pattern.compile("[0-9]*");    
  105.              Matcher isNum = pattern.matcher(str);    
  106.              if (isNum.matches()) {    
  107.                  return true;    
  108.              } else {    
  109.                  return false;    
  110.              }    
  111.          }  
  112.           
  113.        
  114.   
  115.         /**验证日期字符串是否是YYYY-MM-DD格式 
  116.           * @param str 
  117.           * @return 
  118.           */  
  119.         public static boolean isDataFormat(String str){  
  120.           boolean flag=false;  
  121.            //String regxStr="[1-9][0-9]{3}-[0-1][0-2]-((0[1-9])|([12][0-9])|(3[01]))";  
  122.           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])))))?$";  
  123.           Pattern pattern1=Pattern.compile(regxStr);  
  124.           Matcher isNo=pattern1.matcher(str);  
  125.           if(isNo.matches()){  
  126.             flag=true;  
  127.           }  
  128.           return flag;  
  129.        }  
  130.   
  131.      //身份证号码验证:end  
  132.       
  133.       
  134. }  
1 0