父类 ValidatorParent
```
import java.util.regex.Pattern;
/**
* @author qingjiaqi
* @date 6/26/21 10:09 AM
*/
public class ValidatorParent {
/**
* 正则表达式:验证用户名(不包含中文和特殊字符)如果用户名使用手机号码或和邮箱验证
*/
public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{5,17}$";
/**
* 正则表达式:验证密码(不包含特殊字符)
*/
public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,16}$";
/**
* 说明:移动:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188
* 联通:130、131、132、152、155、156、185、186
* 电信:133、153、180、189
* 总结起来就是第一位必定为1,第二位必定为3或5或8,其他位置的可以为0-9
* 验证号码 手机号 固话均可
* 正则表达式:验证手机号
*/
public static final String REGEX_MOBILE = "((^(11|12|13|15|16|18|17|19|14||11|12|16)[0-9]{9}$)|(^0[1,2]{1}\\d{1}-?\\d{8}$)|(^0[3-9] {1}\\d{2}-?\\d{7,8}$)|(^0[1,2]{1}\\d{1}-?\\d{8}-(\\d{1,4})$)|(^0[3-9]{1}\\d{2}-? \\d{7,8}-(\\d{1,4})$))";
/**
* 正则表达式:验证邮箱
*/
public static final String REGEX_EMAIL = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
/**
* 正则表达式:验证汉字(1-9个汉字) {1,9} 自定义区间
*/
public static final String REGEX_CHINESE = "^[\u4e00-\u9fa5]{1,9}$";
/**
* 正则表达式:验证字符串是否全是数字
*/
public static final String REGEX_NUMBER = "^[-\\+]?[\\d]*$";
/**
* 正则:qq号
*/
public static final String REGEX_QQ = "^[1-9][0-9]{4,14}";
/**
* 正则: 微信号
*/
public static final String REGEX_WECHAT = "^[a-zA-Z0-9_-]{5,19}$";
/**
* 正则表达式:验证身份证
*/
public static final String REGEX_ID_CARD = "(\\d{14}[0-9a-zA-Z])|(\\d{17}[0-9a-zA-Z])";
/**
* 正则表达式:验证URL
*/
public static final String REGEX_URL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
/**
* 正则表达式:验证IP地址
*/
public static final String REGEX_IP_ADDR = "(2[5][0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})\\.(25[0-5]|2[0-4]\\d|1\\d{2}|\\d{1,2})";
/**
* 验证是否为YYYYMMDD格式日期
*/
public static final Pattern REX_DATE_YYYYMMDD_PATTERN = Pattern.compile("^[0-9]{8}$");
/**
* 验证是否为YYYYMM格式日期
*/
public static final Pattern REX_DATE_YYYYMM_PATTERN = Pattern.compile("^[0-9]{6}$");
/**
* 身份证校验加权因子
*/
public static final Integer[] ID_NUM_FACTOR = new Integer[]{7, 9, 10, 5, 8, 4,
2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};
/**
* 身份证第18位校验码
*/
public static final String[] ID_NUM_PARITY_BIT = new String[]{"1", "0", "X", "9",
"8", "7", "6", "5", "4", "3", "2"};
/**
* 判断是否为“”式的时期
*
* @param dateStr
* @return
*/
public static boolean isDateComma(String dateStr) {
if (StringUtils.isEmpty(dateStr)
|| !REX_DATE_YYYYMM_PATTERN.matcher(dateStr).matches()) {
return false;
}
return isValidDateRange(dateCommaSplit(dateStr));
}
/**
* split date 0-YY,1-MM,2-DD
*
* @param dateStr
* @return
*/
private static Integer[] dateCommaSplit(String dateStr) {
final Integer YEAR_BASE = 1900;
Integer year = null, month = null, day = null;
year = YEAR_BASE + Integer.valueOf(dateStr.substring(0, 2));
month = Integer.valueOf(dateStr.substring(2, 4));
day = Integer.valueOf(dateStr.substring(4, 6));
return new Integer[]{year, month, day};
}
/**
* 判断是否为“YYYYMMDD”式的时期
*
* @param dateStr
* @return
*/
public static boolean isDateYMD(String dateStr) {
if (StringUtils.isEmpty(dateStr)
|| !REX_DATE_YYYYMMDD_PATTERN.matcher(dateStr).matches()) {
return false;
}
return isValidDateRange(date8Split(dateStr));
}
/**
* 是有效日期范围
*
* @param dateSplitResult
* @return
*/
private static boolean isValidDateRange(Integer[] dateSplitResult) {
Integer year = dateSplitResult[0], month = dateSplitResult[1], day = dateSplitResult[2];
if (isInvalidYear(year)) {
return false;
}
if (isInvalidMonth(month)) {
return false;
}
return !isInvalidDay(day, month, year);
}
public static boolean isInvalidYear(Integer year) {
return year < 1700 || year > 2500;
}
public static boolean isInvalidMonth(Integer month) {
return month < 1 || month > 12;
}
public static boolean isInvalidDay(Integer day, Integer month, Integer year) {
Integer[] iaMonthDays = new Integer[]{31, 28, 31, 30, 31, 30, 31, 31,
30, 31, 30, 31};
if (isLeapYear(year)) {
iaMonthDays[1] = 29;
}
return day < 1 || day > iaMonthDays[month - 1];
}
public static boolean isLeapYear(Integer year) {
return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}
/**
* split date 0-YYYY,1-MM,2-DD
*
* @param dateStr
* @return
*/
private static Integer[] date8Split(String dateStr) {
Integer year = null, month = null, day = null;
year = Integer.valueOf(dateStr.substring(0, 4));
month = Integer.valueOf(dateStr.substring(4, 6));
if (dateStr.length() == 8) {
day = Integer.valueOf(dateStr.substring(6, 8));
return new Integer[]{year, month, day};
} else {
return new Integer[]{year, month};
}
}
}
```
子类 ValidatorUtil.class
```
import java.util.regex.Pattern;
/**
* @author qingjiaqi
* @date 6/26/21 10:11 AM
*/
public class ValidatorUtil extends ValidatorParent {
/**
* 校验用户名
*
* @param username
* @return 校验通过返回true,否则返回false
*/
public static boolean isUserName(String username) {
return Pattern.matches(REGEX_USERNAME, username);
}
/**
* 校验密码
*
* @param password
* @return 校验通过返回true,否则返回false
*/
public static boolean isPassword(String password) {
return Pattern.matches(REGEX_PASSWORD, password);
}
/**
* 校验手机号
*
* @param mobile
* @return 校验通过返回true,否则返回false
*/
public static boolean isMobile(String mobile) {
return Pattern.matches(REGEX_MOBILE, mobile);
}
/**
* 校验邮箱
*
* @param email
* @return 校验通过返回true,否则返回false
*/
public static boolean isEmail(String email) {
return Pattern.matches(REGEX_EMAIL, email);
}
/**
* 校验汉字
*
* @param chinese
* @return 校验通过返回true,否则返回false
*/
public static boolean isChinese(String chinese) {
return Pattern.matches(REGEX_CHINESE, chinese);
}
/**
* 校验身份证 - 只判断位数
*
* @param idCard
* @return 校验通过返回true,否则返回false
*/
public static boolean isIDCard(String idCard) {
return Pattern.matches(REGEX_ID_CARD, idCard);
}
/**
* 校验身份证 - 是否为数字
*
* @param number
* @return 校验通过返回true,否则返回false
*/
public static boolean isNumber(String number) {
return Pattern.matches(REGEX_NUMBER, number);
}
/**
* 校验身份证 - QQ号
*
* @param number
* @return 校验通过返回true,否则返回false
*/
public static boolean isQQ(String number) {
return Pattern.matches(REGEX_QQ, number);
}
/**
* 校验身份证 - QQ号
*
* @param number
* @return 校验通过返回true,否则返回false
*/
public static boolean isWeChat(String number) {
return Pattern.matches(REGEX_WECHAT, number);
}
/**
* 校验URL
*
* @param url
* @return 校验通过返回true,否则返回false
*/
public static boolean isUrl(String url) {
return Pattern.matches(REGEX_URL, url);
}
/**
* 校验IP地址
*
* @param ipAddress
* @return
*/
public static boolean isIPAddress(String ipAddress) {
return Pattern.matches(REGEX_IP_ADDR, ipAddress);
}
/**
* 18位/15位身份证号码校验 - 更准确,身份证校验加权因子
*
* @param idNumber
* @return
*/
public static boolean isIdentityCardNum(String idNumber) {
if (StringUtils.isEmpty(idNumber)
|| (idNumber.length() != 18 && idNumber.length() != 15)) {
return false;
}
// initialize
if (idNumber.length() == 18) {
// check date
String date8 = idNumber.substring(6, 14);
if (isDateYMD(date8) == false) {
return false;
}
int totalMulAiWi = 0;
char charAt;
// check and set value, calculate the totalmulAiWi
for (int i = 0; i < 17; i++) {
charAt = idNumber.charAt(i);
if (charAt < '0' || charAt > '9') {
return false;
}
totalMulAiWi += Integer.valueOf(String.valueOf(charAt)) * ID_NUM_FACTOR[i];
}
// calculate the check digit
String checkDigit = ID_NUM_PARITY_BIT[totalMulAiWi % 11];
// check last digit
if (!checkDigit
.equalsIgnoreCase(String.valueOf(idNumber.charAt(17)))) {
return false;
}
} else {// length is 15
// check date
String date6 = idNumber.substring(6, 12);
if (isDateComma(date6) == false) {
return false;
}
}
return true;
}
}
```

JAVA正则格式校验