新增正则验证规则

This commit is contained in:
wenc000 2020-03-17 22:19:47 +08:00
parent 9536e96790
commit 6631d57c55
2 changed files with 58 additions and 6 deletions

View File

@ -47,6 +47,18 @@ public class RegexUtil {
* 身份证
*/
private static final Pattern PATTERN_IDENTITY = Pattern.compile("(^\\d{15}$)|(^\\d{17}(x|X|\\d)$)");
/**
* 字母
*/
private static final Pattern PATTERN_LETTER = Pattern.compile("[a-zA-Z]+");
/**
* 中文
*/
private static final Pattern PATTERN_CHINESE = Pattern.compile("[\\u4e00-\\u9fa5]+");
/**
* 数字
*/
private static final Pattern PATTERN_NUMBER = Pattern.compile("\\d+");
/**
* 弱密码
*/
@ -140,6 +152,36 @@ public class RegexUtil {
return PATTERN_IDENTITY.matcher(input).matches();
}
/**
* 判断是字母
*
* @param input
* @return
*/
public static boolean isLetter(String input) {
return PATTERN_LETTER.matcher(input).matches();
}
/**
* 判断是中文
*
* @param input
* @return
*/
public static boolean isChinese(String input) {
return PATTERN_CHINESE.matcher(input).matches();
}
/**
* 判断是数字
*
* @param input
* @return
*/
public static boolean isNumber(String input) {
return PATTERN_NUMBER.matcher(input).matches();
}
/**
* 自定义判断
*

View File

@ -138,32 +138,42 @@ public class AnnotationUtil {
}
if (StringUtils.equals("phone", verifyType)) {
if (!RegexUtil.isPhone(value)) {
throw new ParamsException(String.format("%s格式手机格式", name));
throw new ParamsException(String.format("%s格式手机格式", name));
}
return;
} else if (StringUtils.equals("email", verifyType)) {
if (!RegexUtil.isEmail(value)) {
throw new ParamsException(String.format("%s格式邮件格式", name));
throw new ParamsException(String.format("%s格式邮件格式", name));
}
return;
} else if (StringUtils.equals("url", verifyType)) {
if (!RegexUtil.isUrl(value)) {
throw new ParamsException(String.format("%s格式url格式", name));
throw new ParamsException(String.format("%s格式url格式", name));
}
return;
} else if (StringUtils.equals("number", verifyType)) {
if (!NumberUtils.isNumber(value)) {
throw new ParamsException(String.format("%s格式数字格式", name));
throw new ParamsException(String.format("%s格式数字格式", name));
}
return;
} else if (StringUtils.equals("date", verifyType)) {
if (!RegexUtil.isDate(value)) {
throw new ParamsException(String.format("%s格式日期格式", name));
throw new ParamsException(String.format("%s格式日期格式", name));
}
return;
} else if (StringUtils.equals("identity", verifyType)) {
if (!RegexUtil.isIdentity(value)) {
throw new ParamsException(String.format("%s格式不身份证格式", name));
throw new ParamsException(String.format("%s格式非身份证格式", name));
}
return;
} else if (StringUtils.equals("letter", verifyType)) {
if (!RegexUtil.isLetter(value)) {
throw new ParamsException(String.format("%s格式非字母格式", name));
}
return;
} else if (StringUtils.equals("chinese", verifyType)) {
if (!RegexUtil.isLetter(value)) {
throw new ParamsException(String.format("%s格式非中文格式", name));
}
return;
} else if (StringUtils.equals("custom", verifyType)) {