身份证验证有无代码

package com;

public class PasswordCheck {

public static boolean validateIDCard(String IDCard) {
boolean isValidate = false;
int addNum = 0;

String reg = "((11|12|13|14|15|21|22|23|31|32|33|34|35|36|37|41|42|43|44|45|46|50|51|52|53|54|61|62|63|64|65)[0-9]{4})(([1|2|3][0-9]{3}[0|1][0-9][0-3][0-9][0-9]{3}[X0-9])|([0-9]{2}[0|1][0-9][0-3][0-9][0-9]{3}))";
if (IDCard.length() == 18 || IDCard.length() == 15) {
isValidate = IDCard.matches(reg);
} else {
isValidate = false;
}

int key[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1 };
if (IDCard.trim().length() == 18) {
for (int k = 0; k < 18; k++) {
char c = IDCard.charAt(k);
int j = 0;
if (c == 'X') {
j = 10;
} else if (c <= '9' || c >= '0') {
j = c-48;
//System.out.println(j);
} else {
isValidate = false;
}
addNum += j * key[k];
}
if (addNum % 11 == 1) {
isValidate = true;
} else {
isValidate = false;
}
}
return isValidate;
}

public static void main(String[] args) {
String idCard="";//要验证的身份证号码
System.out.println(validateIDCard(idCard));
}

}

posted @ 2018-12-27 09:49  PZHZC  阅读(142)  评论(0)    收藏  举报