public class DataCheck {
/**
* 验证字符串是否是手机号码
*
* @param string
* 要验证的字符串
* @return
*/
public static boolean isPhoneNumber(String string) {
// 手机号
Pattern pattern = Pattern.compile("^(13[0-9]|14[57]|15[012356789]|17[013678]|18[0-9])[0-9]{8}$");
Matcher matcher = pattern.matcher(string);
if (matcher.matches()) {
return true;
}
return false;
}
/**
* 检查银行卡(Luhm校验) 1、从卡号最后一位数字开始,逆向将奇数位(1、3、5等等)相加。
* 2、从卡号最后一位数字开始,逆向将偶数位数字,先乘以2(如果乘积为两位数,则将其减去9),再求和。
* 3、将奇数位总和加上偶数位总和,结果应该可以被10整除。
*
* @param cardNo
* @return
*/
public static boolean checkBankCardNo(String cardNo) {
if (StringUtils.isNotEmpty(cardNo)) {
try {
int luhmSum = 0;
int num = 0;
int index = 1;// 逆向后奇偶标志
for (int i = cardNo.length() - 1; i >= 0; i--) {
num = Integer.parseInt(cardNo.charAt(i) + "");
if (index % 2 == 0) {
num = num * 2 > 9 ? num * 2 - 9 : num * 2;
}
luhmSum += num;
index++;
}
return luhmSum % 10 == 0;
} catch (Exception ex) {}
}
return false;
}
/**
* 检查身份证号码
*
* @param idCardNo
* @return
*/
public static boolean checkIdCardNo(String idCardNo) {
if (StringUtils.isNotEmpty(idCardNo)) {
idCardNo = idCardNo.toUpperCase();
int sum = 0;
int[] tempNum = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
String[] code = { "1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2" };
if (idCardNo != null && idCardNo.length() == 18) {
try {
for (int i = 0; i < 17; i++) {
sum += Integer.parseInt(idCardNo.substring(i, i + 1)) * tempNum[i];
}
if (idCardNo.substring(17, 18).equals(code[sum % 11])) {
return true;
}
} catch (Exception e) {}
}
}
return false;
}
}