身份证格式认证

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值