身份证号验证

java 身份证校验,代码中年份判断不够完善,其他编程方式不做考究

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
public class IdUtils {
private static final int[] weight = new int[] { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 }; // 十七位数字本体码权重
private static final char[] verify = new char[] { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' }; // mod11,对应校验码字符值
// /**
// * 保存地区编码
// */
// private static final Map<String, String> map = new HashMap<String,
// String>();
// static {
// map.put("11", "北京");
// map.put("12", "天津");
// map.put("13", "河北");
// map.put("14", "山西");
// map.put("15", "内蒙古");
// map.put("21", "辽宁");
// map.put("22", "吉林");
// map.put("23", "黑龙江");
// map.put("31", "上海");
// map.put("32", "江苏");
// map.put("33", "浙江");
// map.put("34", "安徽");
// map.put("35", "福建");
// map.put("36", "江西");
// map.put("37", "山东");
// map.put("41", "河南");
// map.put("42", "湖北");
// map.put("43", "湖南");
// map.put("44", "广东");
// map.put("45", "广西");
// map.put("46", "海南");
// map.put("50", "重庆");
// map.put("51", "四川");
// map.put("52", "贵州");
// map.put("53", "云南");
// map.put("54", "西藏");
// map.put("61", "陕西");
// map.put("62", "甘肃");
// map.put("63", "青海");
// map.put("64", "宁夏");
// map.put("65", "新疆");
// map.put("71", "台湾");
// map.put("81", "香港");
// map.put("82", "澳门");
// map.put("91", "国外");
// }
/**
* 验证身份证
*/
public static MyObj verifyIdcard(String idstr) {
MyObj rt = new MyObj();
try {
// 1、校验是否为空
if (idstr == null || idstr == "") {
throw new MyException(50001, "校验的身份证不能为空!");
}
// 2、校验长度
String idstr17 = "";
int length = idstr.length();
if (length == 15) {
idstr17 = idstr.substring(0, 6) + "19" + idstr.substring(6, length);
} else if (length == 18) {
idstr17 = idstr.substring(0, 17);
} else {
throw new MyException(50002, "校验的身份证长度不符合,校验的身份证长度只能是15位或者18位");
}
// 3、校验地区编码
// verifyAreacode(idstr17);
// 4、校验数字
verifyNumeric(idstr17);
// 5、校验日期有效性
verifyDate(idstr17);
// 6、获取身份证校验码,比较2个身份证号码是否一致
char verifyCode = getVerifyCode(idstr17);
String verifyStr = idstr17 + verifyCode; // 通过校验得到的身份证号码
if (!verifyStr.equals(idstr)) {
throw new MyException(50006, "身份证无效,不是合法的身份证号码");
}
} catch (MyException e) {
rt.setErrcode(e.getErrcode());
rt.setErrmsg(e.getErrmsg());
}
return rt;
}
// /**
// * 校验地区编码:身份证前2位
// *
// * @param idstr17
// * 身份证前17位
// */
// private static void verifyAreacode(String idstr17)
// throws ErrException {
// String acode = idstr17.substring(6, 10);// 年份
// if (map.get(acode) == null) {
// throw new ErrException(50003, "身份证地区编码错误(" + acode + ")");
// }
// }
/**
* 校验身份证前17位是否有效数字
*
* @param idtstr17
* 身份证前17位
* @return 数字-true,非数字-false
*/
private static void verifyNumeric(String idtstr17) throws MyException {
if (!idtstr17.matches("^\\d{17}$")) {
throw new MyException(50004, "身份证15位号码,18位号码除最后一位外,都应为数字。");
}
}
/**
* 身份证校验码
*
* @param idstr17
* 身份证前17位
* @return 返回最后一位效验码
*/
private static char getVerifyCode(String idstr17) {
int sum = 0;
int mode = 0;
for (int i = 0; i < idstr17.length(); i++) {
sum += ((int) (idstr17.charAt(i) - 0x30)) * weight[i]; // 0x30:16进制的0
}
mode = sum % 11;
return verify[mode];
}
/**
* 校验生日日期是否正确
*
* @param idstr17
* 身份证前17位
* @return 返回最后一位效验码
*/
private static void verifyDate(String idstr17) throws MyException {
String year = idstr17.substring(6, 10);// 年份
String month = idstr17.substring(10, 12);// 月份
String day = idstr17.substring(12, 14);// 月份
String str = year + "-" + month + "-" + day;
// TODO 用“(19|2\\d)\\d{2}”来验证年份,有欠完善性
if (!str.matches("^(19|2\\d)\\d{2}-((0[1-9])|(1[012]))-((0[1-9])|(1\\d)|(2\\d)|(3[01]))$")) {
throw new MyException(50005, "身份证生日无效(" + str + ")!");
}
}
public static void main(String[] args) throws Exception {
String str = "";
// System.out.println("身份证校验码:" + IdUtils.getVerifyCode(str.substring(0,
// 17)));
System.out.println("身份证校验:" + IdUtils.verifyIdcard(str));
}
}
class MyObj {
private int errcode = 0;
private String errmsg = "ok";
public MyObj() {
}
public MyObj(int errcode, String errmsg) {
this.errcode = errcode;
this.errmsg = errmsg;
}
public int getErrcode() {
return errcode;
}
public void setErrcode(int errcode) {
this.errcode = errcode;
}
public String getErrmsg() {
return errmsg;
}
public void setErrmsg(String errmsg) {
this.errmsg = errmsg;
}
@Override
public String toString() {
return "MyObj [errcode=" + errcode + ", errmsg=" + errmsg + "]";
}
}
class MyException extends Exception {
private static final long serialVersionUID = -1756360434352093459L;
private int errcode = 0;
private String errmsg = "ok";
public MyException() {
super();
}
public MyException(Throwable cause) {
super(cause);
}
public MyException(int errcode, String errmsg) {
super(errmsg);
this.errcode = errcode;
this.errmsg = errmsg;
}
public MyException(int errcode, Throwable cause) {
super(cause);
this.errcode = errcode;
}
public MyException(String errmsg, Throwable cause) {
super(errmsg, cause);
this.errmsg = errmsg;
}
public MyException(int errcode, String errmsg, Throwable cause) {
super(errmsg, cause);
this.errcode = errcode;
this.errmsg = errmsg;
}
public int getErrcode() {
return errcode;
}
public void setErrcode(int errcode) {
this.errcode = errcode;
}
public String getErrmsg() {
return errmsg;
}
public void setErrmsg(String errmsg) {
this.errmsg = errmsg;
}
@Override
public String toString() {
return "MyException [errcode=" + errcode + ", errmsg=" + errmsg + "]";
}
}
  • 本文作者: forever杨
  • 本文链接: https://blog.yl-online.top/posts/1af78879.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。如果文章内容对你有用,请记录到你的笔记中。本博客站点随时会停止服务,请不要收藏、转载!