zoukankan      html  css  js  c++  java
  • Java进行身份证格式强校验(准)

    最近做了一个系统,涉及到对用户输入的身份证号进行校验,减少脏数据传入后台处理并降低企业验证成本,因此在接入层便对输入信息做格式强校验。

    直接附上代码,可直接使用。

     1 package hope.identitycodecheck.demo;
     2 
     3 import java.text.DateFormat;
     4 import java.text.SimpleDateFormat;
     5 import java.util.Date;
     6 /**
     7  * 
     8  * @author hp
     9  * 
    10  * */
    11 public class IdentityCodeUtil {
    12     /**
    13      * 身份证号校验 (支持18位)
    14      * 
    15      * */
    16     public static boolean checkIdentityCode(String identityCode) {
    17         if (!identityCode.matches("\d{17}(\d|x|X)$")) {
    18             return false;
    19         }
    20         Date d = new Date();
    21         DateFormat df = new SimpleDateFormat("yyyyMMdd");
    22         int year = Integer.parseInt(df.format(d));
    23         if (Integer.parseInt(identityCode.substring(6, 10)) < 1900 || Integer.parseInt(identityCode.substring(6, 10)) > year) {// 7-10位是出生年份,范围应该在1900-当前年份之间
    24             return false;
    25         }
    26         if (Integer.parseInt(identityCode.substring(10, 12)) < 1 || Integer.parseInt(identityCode.substring(10, 12)) > 12) {// 11-12位代表出生月份,范围应该在01-12之间
    27             return false;
    28         }
    29         if (Integer.parseInt(identityCode.substring(12, 14)) < 1 || Integer.parseInt(identityCode.substring(12, 14)) > 31) {// 13-14位是出生日期,范围应该在01-31之间
    30             return false;
    31         }
    32         // 校验第18位
    33         // S = Sum(Ai * Wi), i = 0, ... , 16 ,先对前17位数字的权求和
    34         // Ai:表示第i位置上的身份证号码数字值
    35         // Wi:表示第i位置上的加权因子
    36         // Wi: 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
    37         String[] tempA = identityCode.split("|");
    38         int[] a = new int[18];
    39         for (int i = 0; i < tempA.length - 2; i++) {
    40             a[i] = Integer.parseInt(tempA[i + 1]);
    41         }
    42         int[] w = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; // 加权因子
    43         int sum = 0;
    44         for (int i = 0; i < 17; i++) {
    45             sum = sum + a[i] * w[i];
    46         }
    47         // Y = mod(S, 11)
    48         // 通过模得到对应的校验码
    49         // Y: 0 1 2 3 4 5 6 7 8 9 10
    50         // 校验码: 1 0 X 9 8 7 6 5 4 3 2
    51         String[] v = { "1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2" }; // 校验码
    52         int y = sum % 11;
    53         if (!v[y].equalsIgnoreCase(identityCode.substring(17))) {// 第18位校验码错误
    54             return false;
    55         }
    56         return true;
    57     }
    58     
    59 
    60     public static void main(String[] args) {
    61         System.out.println(checkIdentityCode("110110198001019719"));
    62     }
    63 }

    希望可以为大家提供方便和帮助。

  • 相关阅读:
    SpringCloud Eureka的一些问题
    git branch---删除命令
    go:cannot find main module;see 'go help modules'
    ERROR: yaml.scanner.ScannerError: while scanning for the next token found character ' ' that cannot
    vscode 打开wsl2:ubuntu中的文件进行编辑保存提示没有权限
    Ubuntu20.04 wechat和qq 字体太小,乱码 #
    ubuntu20.4 微信安装
    centos7中在环境变量中添加了GO的PATH,但依然无效
    直接把linux装在了物理机上
    win10 + linux 双系统安装
  • 原文地址:https://www.cnblogs.com/orionhp/p/6362653.html
Copyright © 2011-2022 走看看