首页

基于hutool工具包的Validator校验器手机、邮箱、生日、url、金钱、mac、uuid及中文等

标签:校验手机,校验邮箱,校验生日,校验url,校验金钱,校验mac,校验uuid,校验中文     发布时间:2022-07-28   

一、前言

参考hutoolhutool-all包的cn.hutool.core.lang.Validator校验器类,对手机、邮箱、生日、url、金钱、mac、uuid及中文等进行有效性校验判断。

二、代码说明

package cn.hutool.core.lang;@b@@b@import cn.hutool.core.date.DateUtil;@b@import cn.hutool.core.exceptions.ValidateException;@b@import cn.hutool.core.util.NumberUtil;@b@import cn.hutool.core.util.ObjectUtil;@b@import cn.hutool.core.util.ReUtil;@b@import cn.hutool.core.util.StrUtil;@b@import java.net.MalformedURLException;@b@import java.net.URL;@b@import java.util.regex.Pattern;@b@@b@public class Validator {@b@    public static final Pattern GENERAL;@b@    public static final Pattern NUMBERS;@b@    public static final Pattern GROUP_VAR;@b@    public static final Pattern IPV4;@b@    public static final Pattern MONEY;@b@    public static final Pattern EMAIL;@b@    public static final Pattern MOBILE;@b@    public static final Pattern CITIZEN_ID;@b@    public static final Pattern ZIP_CODE;@b@    public static final Pattern BIRTHDAY;@b@    public static final Pattern URL;@b@    public static final Pattern URL_HTTP;@b@    public static final Pattern GENERAL_WITH_CHINESE;@b@    public static final Pattern UUID;@b@    public static final Pattern UUID_SIMPLE;@b@    public static final Pattern PLATE_NUMBER;@b@@b@    private Validator() {@b@    }@b@@b@    public static boolean isNull(Object value) {@b@        return null == value;@b@    }@b@@b@    public static boolean isNotNull(Object value) {@b@        return null != value;@b@    }@b@@b@    public static <T> T validateNotNull(T value, String errorMsgTemplate, Object... params) throws ValidateException {@b@        if (isNull(value)) {@b@            throw new ValidateException(errorMsgTemplate, params);@b@        } else {@b@            return value;@b@        }@b@    }@b@@b@    public static boolean isEmpty(Object value) {@b@        return null == value || value instanceof String && StrUtil.isEmpty((String)value);@b@    }@b@@b@    public static boolean isNotEmpty(Object value) {@b@        return !isEmpty(value);@b@    }@b@@b@    public static void validateNotEmpty(Object value, String errorMsg) throws ValidateException {@b@        if (isEmpty(value)) {@b@            throw new ValidateException(errorMsg);@b@        }@b@    }@b@@b@    public static boolean equal(Object t1, Object t2) {@b@        return ObjectUtil.equal(t1, t2);@b@    }@b@@b@    public static void validateEqual(Object t1, Object t2, String errorMsg) throws ValidateException {@b@        if (!equal(t1, t2)) {@b@            throw new ValidateException(errorMsg);@b@        }@b@    }@b@@b@    public static void validateNotEqual(Object t1, Object t2, String errorMsg) throws ValidateException {@b@        if (equal(t1, t2)) {@b@            throw new ValidateException(errorMsg);@b@        }@b@    }@b@@b@    public static void validateNotEmptyAndEqual(Object t1, Object t2, String errorMsg) throws ValidateException {@b@        validateNotEmpty(t1, errorMsg);@b@        validateEqual(t1, t2, errorMsg);@b@    }@b@@b@    public static void validateNotEmptyAndNotEqual(Object t1, Object t2, String errorMsg) throws ValidateException {@b@        validateNotEmpty(t1, errorMsg);@b@        validateNotEqual(t1, t2, errorMsg);@b@    }@b@@b@    public static boolean isMactchRegex(String regex, String value) {@b@        return ReUtil.isMatch(regex, value);@b@    }@b@@b@    public static void validateMatchRegex(String regex, String value, String errorMsg) throws ValidateException {@b@        if (!isMactchRegex(regex, value)) {@b@            throw new ValidateException(errorMsg);@b@        }@b@    }@b@@b@    public static boolean isMactchRegex(Pattern pattern, String value) {@b@        return ReUtil.isMatch(pattern, value);@b@    }@b@@b@    public static boolean isGeneral(String value) {@b@        return isMactchRegex(GENERAL, value);@b@    }@b@@b@    public static void validateGeneral(String value, String errorMsg) throws ValidateException {@b@        if (!isGeneral(value)) {@b@            throw new ValidateException(errorMsg);@b@        }@b@    }@b@@b@    public static boolean isGeneral(String value, int min, int max) {@b@        String reg = "^\\w{" + min + "," + max + "}$";@b@        if (min < 0) {@b@            min = 0;@b@        }@b@@b@        if (max <= 0) {@b@            reg = "^\\w{" + min + ",}$";@b@        }@b@@b@        return isMactchRegex(reg, value);@b@    }@b@@b@    public static void validateGeneral(String value, int min, int max, String errorMsg) throws ValidateException {@b@        if (!isGeneral(value, min, max)) {@b@            throw new ValidateException(errorMsg);@b@        }@b@    }@b@@b@    public static boolean isGeneral(String value, int min) {@b@        return isGeneral(value, min, 0);@b@    }@b@@b@    public static void validateGeneral(String value, int min, String errorMsg) throws ValidateException {@b@        validateGeneral(value, min, 0, errorMsg);@b@    }@b@@b@    public static boolean isLetter(String value) {@b@        return StrUtil.isAllCharMatch(value, new Matcher<Character>() {@b@            public boolean match(Character t) {@b@                return Character.isLetter(t);@b@            }@b@        });@b@    }@b@@b@    public static void validateLetter(String value, String errorMsg) throws ValidateException {@b@        if (!isLetter(value)) {@b@            throw new ValidateException(errorMsg);@b@        }@b@    }@b@@b@    public static boolean isUpperCase(String value) {@b@        return StrUtil.isAllCharMatch(value, new Matcher<Character>() {@b@            public boolean match(Character t) {@b@                return Character.isUpperCase(t);@b@            }@b@        });@b@    }@b@@b@    public static void validateUpperCase(String value, String errorMsg) throws ValidateException {@b@        if (!isUpperCase(value)) {@b@            throw new ValidateException(errorMsg);@b@        }@b@    }@b@@b@    public static boolean isLowerCase(String value) {@b@        return StrUtil.isAllCharMatch(value, new Matcher<Character>() {@b@            public boolean match(Character t) {@b@                return Character.isLowerCase(t);@b@            }@b@        });@b@    }@b@@b@    public static void validateLowerCase(String value, String errorMsg) throws ValidateException {@b@        if (!isLowerCase(value)) {@b@            throw new ValidateException(errorMsg);@b@        }@b@    }@b@@b@    public static boolean isNumber(String value) {@b@        return NumberUtil.isNumber(value);@b@    }@b@@b@    public static void validateNumber(String value, String errorMsg) throws ValidateException {@b@        if (!isNumber(value)) {@b@            throw new ValidateException(errorMsg);@b@        }@b@    }@b@@b@    public static boolean isWord(String value) {@b@        return isMactchRegex(PatternPool.WORD, value);@b@    }@b@@b@    public static void validateWord(String value, String errorMsg) throws ValidateException {@b@        if (!isWord(value)) {@b@            throw new ValidateException(errorMsg);@b@        }@b@    }@b@@b@    public static boolean isMoney(String value) {@b@        return isMactchRegex(MONEY, value);@b@    }@b@@b@    public static void validateMoney(String value, String errorMsg) throws ValidateException {@b@        if (!isMoney(value)) {@b@            throw new ValidateException(errorMsg);@b@        }@b@    }@b@@b@    public static boolean isZipCode(String value) {@b@        return isMactchRegex(ZIP_CODE, value);@b@    }@b@@b@    public static void validateZipCode(String value, String errorMsg) throws ValidateException {@b@        if (!isZipCode(value)) {@b@            throw new ValidateException(errorMsg);@b@        }@b@    }@b@@b@    public static boolean isEmail(String value) {@b@        return isMactchRegex(EMAIL, value);@b@    }@b@@b@    public static void validateEmail(String value, String errorMsg) throws ValidateException {@b@        if (!isEmail(value)) {@b@            throw new ValidateException(errorMsg);@b@        }@b@    }@b@@b@    public static boolean isMobile(String value) {@b@        return isMactchRegex(MOBILE, value);@b@    }@b@@b@    public static void validateMobile(String value, String errorMsg) throws ValidateException {@b@        if (!isMobile(value)) {@b@            throw new ValidateException(errorMsg);@b@        }@b@    }@b@@b@    public static boolean isCitizenId(String value) {@b@        return isMactchRegex(CITIZEN_ID, value);@b@    }@b@@b@    public static void validateCitizenIdNumber(String value, String errorMsg) throws ValidateException {@b@        if (!isCitizenId(value)) {@b@            throw new ValidateException(errorMsg);@b@        }@b@    }@b@@b@    public static boolean isBirthday(int year, int month, int day) {@b@        int thisYear = DateUtil.thisYear();@b@        if (year >= 1900 && year <= thisYear) {@b@            if (month >= 1 && month <= 12) {@b@                if (day >= 1 && day <= 31) {@b@                    if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {@b@                        return false;@b@                    } else {@b@                        return month != 2 || day <= 29 && (day != 29 || DateUtil.isLeapYear(year));@b@                    }@b@                } else {@b@                    return false;@b@                }@b@            } else {@b@                return false;@b@            }@b@        } else {@b@            return false;@b@        }@b@    }@b@@b@    public static boolean isBirthday(String value) {@b@        if (isMactchRegex(BIRTHDAY, value)) {@b@            java.util.regex.Matcher matcher = BIRTHDAY.matcher(value);@b@            if (matcher.find()) {@b@                int year = Integer.parseInt(matcher.group(1));@b@                int month = Integer.parseInt(matcher.group(3));@b@                int day = Integer.parseInt(matcher.group(5));@b@                return isBirthday(year, month, day);@b@            }@b@        }@b@@b@        return false;@b@    }@b@@b@    public static void validateBirthday(String value, String errorMsg) throws ValidateException {@b@        if (!isBirthday(value)) {@b@            throw new ValidateException(errorMsg);@b@        }@b@    }@b@@b@    public static boolean isIpv4(String value) {@b@        return isMactchRegex(IPV4, value);@b@    }@b@@b@    public static void validateIpv4(String value, String errorMsg) throws ValidateException {@b@        if (!isIpv4(value)) {@b@            throw new ValidateException(errorMsg);@b@        }@b@    }@b@@b@    public static boolean isMac(String value) {@b@        return isMactchRegex(PatternPool.MAC_ADDRESS, value);@b@    }@b@@b@    public static void validateMac(String value, String errorMsg) throws ValidateException {@b@        if (!isMac(value)) {@b@            throw new ValidateException(errorMsg);@b@        }@b@    }@b@@b@    public static boolean isPlateNumber(String value) {@b@        return isMactchRegex(PLATE_NUMBER, value);@b@    }@b@@b@    public static void validatePlateNumber(String value, String errorMsg) throws ValidateException {@b@        if (!isPlateNumber(value)) {@b@            throw new ValidateException(errorMsg);@b@        }@b@    }@b@@b@    public static boolean isUrl(String value) {@b@        try {@b@            new URL(value);@b@            return true;@b@        } catch (MalformedURLException var2) {@b@            return false;@b@        }@b@    }@b@@b@    public static void validateUrl(String value, String errorMsg) throws ValidateException {@b@        if (!isUrl(value)) {@b@            throw new ValidateException(errorMsg);@b@        }@b@    }@b@@b@    public static boolean isChinese(String value) {@b@        return isMactchRegex("^[一-\u9fff]+$", value);@b@    }@b@@b@    public static void validateChinese(String value, String errorMsg) throws ValidateException {@b@        if (!isChinese(value)) {@b@            throw new ValidateException(errorMsg);@b@        }@b@    }@b@@b@    public static boolean isGeneralWithChinese(String value) {@b@        return isMactchRegex(GENERAL_WITH_CHINESE, value);@b@    }@b@@b@    public static void validateGeneralWithChinese(String value, String errorMsg) throws ValidateException {@b@        if (!isGeneralWithChinese(value)) {@b@            throw new ValidateException(errorMsg);@b@        }@b@    }@b@@b@    public static boolean isUUID(String value) {@b@        return isMactchRegex(UUID, value) || isMactchRegex(UUID_SIMPLE, value);@b@    }@b@@b@    public static void validateUUID(String value, String errorMsg) throws ValidateException {@b@        if (!isUUID(value)) {@b@            throw new ValidateException(errorMsg);@b@        }@b@    }@b@@b@    public static boolean isBetween(Number value, Number min, Number max) {@b@        Assert.notNull(value);@b@        Assert.notNull(min);@b@        Assert.notNull(max);@b@        double doubleValue = value.doubleValue();@b@        return doubleValue >= min.doubleValue() && doubleValue <= max.doubleValue();@b@    }@b@@b@    public static void validateBetween(Number value, Number min, Number max, String errorMsg) throws ValidateException {@b@        if (!isBetween(value, min, max)) {@b@            throw new ValidateException(errorMsg);@b@        }@b@    }@b@@b@    static {@b@        GENERAL = PatternPool.GENERAL;@b@        NUMBERS = PatternPool.NUMBERS;@b@        GROUP_VAR = PatternPool.GROUP_VAR;@b@        IPV4 = PatternPool.IPV4;@b@        MONEY = PatternPool.MONEY;@b@        EMAIL = PatternPool.EMAIL;@b@        MOBILE = PatternPool.MOBILE;@b@        CITIZEN_ID = PatternPool.CITIZEN_ID;@b@        ZIP_CODE = PatternPool.ZIP_CODE;@b@        BIRTHDAY = PatternPool.BIRTHDAY;@b@        URL = PatternPool.URL;@b@        URL_HTTP = PatternPool.URL_HTTP;@b@        GENERAL_WITH_CHINESE = PatternPool.GENERAL_WITH_CHINESE;@b@        UUID = PatternPool.UUID;@b@        UUID_SIMPLE = PatternPool.UUID_SIMPLE;@b@        PLATE_NUMBER = PatternPool.PLATE_NUMBER;@b@    }@b@}
  • ◆ 相关内容