import java.util.regex.Matcher;
import java.util.regex.Pattern;
//工具类,判断手机号,身份证号,银行卡号
public class ValidateUtil {
public ValidateUtil() {
}
public static boolean isMobileNum(String mobile) {
Pattern pattern = Pattern.compile("^((1(3|4|5|6|7|8|9)[0-9]))\\d{8}$");
Matcher matcher = pattern.matcher(mobile);
return matcher.matches();
}
public static boolean isUserIDCard(String idcard) {
Pattern pattern = Pattern.compile("^(\\d{6})(19|20)(\\d{2})(1[0-2]|0[1-9])(0[1-9]|[1-2][0-9]|3[0-1])(\\d{3})(\\d|X|x)?$");
Matcher matcher = pattern.matcher(idcard);
if (idcard.length() == 18 && matcher.matches()) {
String[] idSplit = idcard.split("");
if ("X".equals(idSplit[17])) {
idSplit[17] = "x";
}
int[] factor = new int[]{7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
String[] parity = new String[]{"1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2"};
int sum = 0;
int ai = false;
int wi = false;
for(int i = 0; i < 17; ++i) {
int ai = Integer.valueOf(idSplit[i]);
int wi = factor[i];
sum += ai * wi;
}
return idSplit[17].equals(parity[sum % 11]);
} else {
return false;
}
}
public static boolean checkBankCard(String bankCard) {
if (bankCard.length() >= 15 && bankCard.length() <= 19) {
char bit = getBankCardCheckCode(bankCard.substring(0, bankCard.length() - 1));
if (bit == 'N') {
return false;
} else {
return bankCard.charAt(bankCard.length() - 1) == bit;
}
} else {
return false;
}
}
public static char getBankCardCheckCode(String nonCheckCodeBankCard) {
if (nonCheckCodeBankCard != null && nonCheckCodeBankCard.trim().length() != 0 && nonCheckCodeBankCard.matches("\\d+")) {
char[] chs = nonCheckCodeBankCard.trim().toCharArray();
int luhmSum = 0;
int i = chs.length - 1;
for(int j = 0; i >= 0; ++j) {
int k = chs[i] - 48;
if (j % 2 == 0) {
k *= 2;
k = k / 10 + k % 10;
}
luhmSum += k;
--i;
}
return luhmSum % 10 == 0 ? '0' : (char)(10 - luhmSum % 10 + 48);
} else {
return 'N';
}
}
}
工具类,判断手机号,身份证号,银行卡号
最新推荐文章于 2021-03-10 18:00:57 发布