新增用户名格式校验

This commit is contained in:
wenc000 2020-05-21 23:47:37 +08:00
parent ab41103a24
commit 8632d9a075
2 changed files with 20 additions and 1 deletions

View File

@ -51,6 +51,10 @@ public class RegexUtil {
* 身份证 * 身份证
*/ */
private static final Pattern PATTERN_IDENTITY = Pattern.compile("(^\\d{15}$)|(^\\d{17}(x|X|\\d)$)"); private static final Pattern PATTERN_IDENTITY = Pattern.compile("(^\\d{15}$)|(^\\d{17}(x|X|\\d)$)");
/**
* 用户名
*/
private static final Pattern PATTERN_USERNAME = Pattern.compile("^[a-zA-Z0-9_\\s]+$");
/** /**
* 字母 * 字母
*/ */
@ -166,6 +170,16 @@ public class RegexUtil {
return PATTERN_IDENTITY.matcher(input).matches(); return PATTERN_IDENTITY.matcher(input).matches();
} }
/**
* 判断用户名
*
* @param input
* @return
*/
public static boolean isUsername(String input) {
return PATTERN_USERNAME.matcher(input).matches();
}
/** /**
* 判断是字母 * 判断是字母
* *

View File

@ -136,7 +136,12 @@ public class AnnotationUtil {
if (StringUtils.isBlank(verifyType)) { if (StringUtils.isBlank(verifyType)) {
return; return;
} }
if (StringUtils.equals("phone", verifyType)) { if (StringUtils.equals("username", verifyType)) {
if (!RegexUtil.isUsername(value)) {
throw new ParamsException(String.format("%s格式只能是字母、数字和下划线", name));
}
return;
} else if (StringUtils.equals("phone", verifyType)) {
if (!RegexUtil.isPhone(value)) { if (!RegexUtil.isPhone(value)) {
throw new ParamsException(String.format("%s格式非手机格式", name)); throw new ParamsException(String.format("%s格式非手机格式", name));
} }