@package com.jkys.cis.promote.common.utils;
import org.apache.commons.lang3.StringUtils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
-
@author 方蝶
-
@Auther: fangDie
-
@Date: 2020/9/3 15:17
-
@Description:
*/
public class CheckUtil {/**
- 返回状态 1为空
*/
public static final String RESULT_NULL = “1”;/**
- 2-不符合规则
*/
public static final String RESULT_Mismatch_RULE = “2”;
/**
- 3-验证通过
*/
public static final String RESULT_TRUE = “3”;
/**
- 校验str中是否包含特殊字符
- @param str 字符
- @return true-有字符 false-没有包含字符
/
public static String checkName(String str) {
//判断姓名是否为空 空-返回false
if(StringUtils.isEmpty(str)){
return RESULT_NULL;
}
String regEx = "[ _`!@#$%^&*()+=|{}’:;’,\[\].<>/?!@#¥%……&()——+|{}【】‘;:”“’。,、?]|\n|\r|\t";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
boolean b = m.find();
//为true姓名包含字符 返回false 不让验证通过
if (b){
return RESULT_Mismatch_RULE;
}
return RESULT_TRUE;
}
/**
-
校验手机号是否1开头并且11位数字
-
@param mobile 手机号
-
@return true-格式正确 false-格式不正确
*/
public static String checkMobile(String mobile){
if (StringUtils.isEmpty(mobile)){
return RESULT_NULL;
}if (!mobile.matches( “1\d{10}”)){
return RESULT_Mismatch_RULE;
}
return RESULT_TRUE;
}
/**
- 校验验证码为四位数字
- @param verifyCode 验证码
- @return true-格式正确 false-格式不正确
*/
public static String checkVerifyCode(String verifyCode){
if (StringUtils.isEmpty(verifyCode)){
return RESULT_NULL;
}
boolean matches = verifyCode.matches("\d{4}");
if (!matches){
return RESULT_Mismatch_RULE;
}
return RESULT_TRUE;
}
public static String checkIdCard(String idCard){
if (StringUtils.isEmpty(idCard)){
return RESULT_NULL;
}
String regularExpression = “(1\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 )|" + "(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3} )∣"+"([1−9]d5d2((0[1−9])∣(10∣11∣12))(([0−2][1−9])∣10∣20∣30∣31)d3)”;
boolean matches = Pattern.matches(regularExpression, idCard);
if (!matches){
return RESULT_Mismatch_RULE;
}
return RESULT_TRUE;
}public static void main(String[] args) {
String idCard=“520202109318107722”;
String s = CheckUtil.checkIdCard(idCard);
System.out.println(s);
}
}
1-9 ↩︎