首先,让我们谈谈一些有关的知识(如果知道,请不要喷洒)
1-2位数字: 代表升级管理区代码
3-4位数字: 代表地级行政区域的分区代码
5-6位数字: 代表县和地区行政区划的代码
7-14位: 代表出生的年,月和日,例如: 19900101
15-17位数字: 代表顺序码,同一区域同一年,同一月和同一天的出生人数,奇数是男性,偶数是女性
18位: 表示校验码,如果是0-9,则用0-9表示,如果是10,则用X(罗马数字10)表示.
注意: 此内实施. 卡号已从原来的15位提高到18位,因此可以确定所有15位ID号码都出生于19xx
这里将不解释前17位数字的验证. 重点是最后一位的验证. 最后一位等于每个数字的权重后前17位乘积的总和,然后除以余数11,这里将等待查看代码说明
前17名的权重如下:
{7,9,10,5,8,4,2,1,6,3,7,9,10,5android+验证号码,8,4,2}
主要代码如下
/**
* 验证结果类
*
*@author liujingxing on 16/07/17.
*/
public class Result {
/**
* 错误消息,为空时,代表验证通过
*/
private String error;
public boolean isLegal() {
//两个变量为默认值,即认为是合法的
return error == null || error.equals("");
}
public String getError() {
return error;
}
public void setError(String message) {
this.error = message;
}
public void show(Context context) {
if (!isLegal())
Toast.makeText(context, error, Toast.LENGTH_SHORT).show();
}
}
/**
* 验证工具,只需要调用静态方法 validateIDNum(String) 传入即可
*
*@author liujingxing by 2016/07/17
*@see #validateIDNum(String)
*/
public class IDCardUtil {
private final static int[] FACTOR = new int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8,
4, 2};
public static Result validateIDNum(String idNum) {
Result result = new Result();// 记录验证结果
//判断是否为空
if (TextUtils.isEmpty(idNum)) {
result.setError("号码不能为空");
return result;
}
//号码的长度只能为15位或18位
int idNumLength = idNum.length();
if (idNumLength != 15 && idNumLength != 18) {
result.setError("号码应该为15位或18位");
return result;
}
//对的字符做判断
if (!isAllNum(idNum)) {
result.setError(idNum.length() == 18 ? "18位号码除最后一位外,都应为数字" : "15位号码都应为数字");
return result;
}
if (idNum.contains("x")) {
result.setError("x必须为大写");
return result;
}
//判断地区编码
Hashtable h = getAreaCode();
if (h.get(idNum.substring(0, 2)) == null) {
result.setError("地区编码错误");
return result;
}
// 出生年月是否有效
String idNum17;
if (idNum.length() == 18) {
idNum17 = idNum.substring(0, 17);
} else {
//如果是15为则加上出生年代:19
idNum17 = idNum.substring(0, 6) + "19" + idNum.substring(6, 15);
}
String strYear = idNum17.substring(6, 10);// 年份
String strMonth = idNum17.substring(10, 12);// 月份
String strDay = idNum17.substring(12, 14);// 月份
if (!validateDate(strYear + "-" + strMonth + "-" + strDay)) {
result.setError("生日无效");
return result;
}
GregorianCalendar gc = new GregorianCalendar();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
try {
if ((gc.get(Calendar.YEAR) - Integer.parseInt(strYear)) > 150 || (gc.getTime().getTime()
- format.parse(strYear + "-" + strMonth + "-" + strDay).getTime()) < 0) {
result.setError("生日不在有效范围");
return result;
}
} catch (NumberFormatException | java.text.ParseException e) {
e.printStackTrace();
}
//18位的对最后一位校验码进行验证
if (idNum.length() == 18 && !isCorrectID(idNum)) {
result.setError("无效,不是合法的号码");
return result;
}
return result;
}
/**
* 功能:设置地区编码
*/
private static Hashtable getAreaCode() {
Hashtable hashTable = new Hashtable<>();
hashTable.put("11", "北京");
hashTable.put("12", "天津");
hashTable.put("13", "河北");
hashTable.put("14", "山西");
hashTable.put("15", "内蒙古");
hashTable.put("21", "辽宁");
hashTable.put("22", "吉林");
hashTable.put("23", "黑龙江");
hashTable.put("31", "上海");
hashTable.put("32", "江苏");
hashTable.put("33", "浙江");
hashTable.put("34", "安徽");
hashTable.put("35", "福建");
hashTable.put("36", "江西");
hashTable.put("37", "山东");
hashTable.put("41", "河南");
hashTable.put("42", "湖北");
hashTable.put("43", "湖南");
hashTable.put("44", "广东");
hashTable.put("45", "广西");
hashTable.put("46", "海南");
hashTable.put("50", "重庆");
hashTable.put("51", "四川");
hashTable.put("52", "贵州");
hashTable.put("53", "云南");
hashTable.put("54", "西藏");
hashTable.put("61", "陕西");
hashTable.put("62", "甘肃");
hashTable.put("63", "青海");
hashTable.put("64", "宁夏");
hashTable.put("65", "新疆");
hashTable.put("71", "台湾");
hashTable.put("81", "香港");
hashTable.put("82", "澳门");
hashTable.put("91", "国外");
return hashTable;
}
/**
* 判断字符是否合法
*/
private static boolean isAllNum(String idNum) {
String match = idNum.length() == 18 ? "^[0-9]{17}([0-9]|X)$" : "^[0-9]{15}$";
Pattern pattern = Pattern.compile(match);
Matcher isNum = pattern.matcher(idNum);
return isNum.matches();
}
/**
* 功能:判断字符串是否为日期格式
*/
private static boolean validateDate(String strDate) {
Pattern pattern = Pattern.compile(
"^((\\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])))))?$");
Matcher m = pattern.matcher(strDate);
return m.matches();
}
/**
* 判断输入的是否合法
*
*@param idNum 18位的号
*@return 合法true,反之false
*/
private static boolean isCorrectID(String idNum) {
boolean flag = false;
if (idNum == null || idNum.trim().length() != 18) {
return false;
}
String last = getLastNumOfID(idNum.substring(0, idNum.length() - 1));
if (last.equals(String.valueOf(idNum.charAt(idNum.length() - 1)))) {
flag = true;
}
return flag;
}
/**
* 根据前17位号,算出第18位数字
*
*@param id17 前17位号
*@return 第18位号对应的数字
*/
private static String getLastNumOfID(String id17) {
int sum = sumFactor(id17);
String res;
if (sum == -1) {
res = "输入的为空";
} else if (sum == -3) {
res = "输入的号码不为17位";
} else {
int mod = sum % 11;
int last = (12 - mod) % 11;
if (last == 10) {
res = "X";//X代表罗马数字10
} else {
res = String.valueOf(last);
}
}
return res;
}
/**
* 计算前17位号乘以各个数的权重的总和
*
*@param id17 前17位号
*@return 权重的总和
*/
private static int sumFactor(String id17) {
if (id17 == null || id17.trim().equals("")) {
return -1; //输入的为空
}
int len = id17.length();
if (len != 17) {
return -3; //输入的号码不为17位
}
int sum = 0;
for (int i = 0; i < len; i++) {
sum += FACTOR[i] * Integer.parseInt(String.valueOf(id17.charAt(i)));
}
return sum;
}
}
上面的代码非常简单,请在此处说明ID验证步骤
第一步: 验证的数量是否足够
第二步: 验证ID卡是否包含数字以外的字符(18位ID卡的最后一个X除外)
第3步: 验证前两个省区代码正确
第4步: 验证7-14个出生日期不在范围内
第5步: 验证最后一个校验位是否正确
如果以上五个步骤中的所有步骤均已通过验证,则可以表明该是合法的. 如果任何验证步骤失败,则会出现相应的提示消息
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/tongxinshuyu/article-244903-1.html