public String saveGreenPerson(HttpServletRequest request) {
String msg = "验证未通过";
String id = request.getParameter("id");
String username = request.getParameter("username");
String tel = request.getParameter("tel");
String identity_card = request.getParameter("identity_card");
String bank_card = request.getParameter("bank_card");
String workShift = request.getParameter("workShift");
String major = request.getParameter("major");
if(!StringFacs.isEmpty(identity_card)){
//校验身份证号码格式通过
// 定义判别用户身份证号的正则表达式(15位或者18位,最后一位可以为字母)
String regularExpression = "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|" +
"(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";
boolean matches = identity_card.matches(regularExpression);
Boolean identityCardRegular=false;
if(identity_card.length()==18){
try {
char[] charArray = identity_card.toCharArray();
//前十七位加权因子
int[] idCardWi = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
//这是除以11后,可能产生的11位余数对应的验证码
String[] idCardY = {"1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"};
int sum = 0;
for (int i = 0; i < idCardWi.length; i++) {
int current = Integer.parseInt(String.valueOf(charArray[i]));
int count = current * idCardWi[i];
sum += count;
}
char idCardLast = charArray[17];
int idCardMod = sum % 11;
if (idCardY[idCardMod].toUpperCase().equals(String.valueOf(idCardLast).toUpperCase())) {
identityCardRegular=true;
}
} catch (Exception e) {
}
}
if(identityCardRegular){
//身份证号不为空,查询是否重复
Map idCard1 = greeningAndWateringService.getIdCard(identity_card);
//没有重复身份证号
if (idCard1.size() == 0 || idCard1 == null) {
//手机号验证通过
if(tel.matches("^1[3|4|5|7|8][0-9]{9}$")){
//校验银行卡号
if(bank_card.length() < 15 || bank_card.length() > 19){
msg="请重新输入银行卡号!";
}else {
char bit = getBankCardCheckCode(bank_card.substring(0, bank_card.length() - 1));
if (bit == 'N') {
msg = "银行卡号格式错误!请重新输入";
} else {
if(bank_card.charAt(bank_card.length() - 1) == bit){
GreenPersonEntity greenPersonEntity = new GreenPersonEntity();
greenPersonEntity.setId(id);
greenPersonEntity.setUsername(username);
greenPersonEntity.setTel(tel);
greenPersonEntity.setIdentityCard(identity_card);
greenPersonEntity.setBankCard(bank_card);
greenPersonEntity.setWorkShift(workShift);
greenPersonEntity.setMajor(major);
appDao.saveGreenPerson(greenPersonEntity);
msg = "验证通过";
}else{
msg = "银行卡号格式错误!请重新输入";
}
}
}
}else{
msg = "手机号格式有误!请重新输入";
}
}else {
msg = "身份证号码重复!请重新输入";
}
}else{
//身份证号码格式错误
msg="请输入正确的身份证号码!";
}
}else{
//身份证号码为空
msg="请输入正确的身份证号码!";
}
return msg;
}
public static char getBankCardCheckCode(String nonCheckCodeCardId) {
if (nonCheckCodeCardId == null || nonCheckCodeCardId.trim().length() == 0
|| !nonCheckCodeCardId.matches("\\d+")) {
//如果传的不是数据返回N
return 'N';
}
char[] chs = nonCheckCodeCardId.trim().toCharArray();
int luhmSum = 0;
for (int i = chs.length - 1, j = 0; i >= 0; i--, j++) {
int k = chs[i] - '0';
if (j % 2 == 0) {
k *= 2;
k = k / 10 + k % 10;
}
luhmSum += k;
}
return (luhmSum % 10 == 0) ? '0' : (char) ((10 - luhmSum % 10) + '0');
}
Java校验身份证、手机号、银行卡号
最新推荐文章于 2024-04-28 10:35:01 发布