wuxw
2024-12-10 35b7b48dcc6825d661c67fb9b7a2ed33d9743851
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package com.java110.utils.util;
 
/**
 * @ClassName Vadator
 * @Description TODO
 * @Author wuxw
 * @Date 2020/2/10 11:00
 * @Version 1.0
 * add by wuxw 2020/2/10
 **/
 
import com.java110.utils.cache.MappingCache;
 
import java.util.regex.Pattern;
 
/**
 * @program:
 * @description: 校验工具类
 * @author: xujingyang
 * @create: 2019-01-05 16:21
 **/
public class ValidatorUtil {
    /**
     * 正则表达式:验证用户名
     */
    public static final String REGEX_USERNAME = "^[a-zA-Z]\\w{5,20}$";
 
    /**
     * 正则表达式:验证密码
     */
    public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,20}$";
 
    /**
     * 正则表达式:验证手机号
     */
    public static final String REGEX_MOBILE = "^(1[0-9])\\d{9}$";
 
    /**
     * 正则表达式:验证邮箱
     */
    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,}$";
 
    /**
     * 正则表达式:验证汉字
     */
    public static final String REGEX_CHINESE = "^[\u4e00-\u9fa5],{0,}$";
 
    /**
     * 正则表达式:验证身份证
     */
    public static final String REGEX_ID_CARD = "(^\\d{18}$)|(^\\d{15}$)";
 
    /**
     * 正则表达式:验证URL
     */
    public static final String REGEX_URL = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
 
    /**
     * 正则表达式:验证IP地址
     */
    public static final String REGEX_IP_ADDR = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";
 
    /**
     * 校验用户名
     *
     * @param username
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isUsername(String username) {
        if (StringUtil.isEmpty(username)) {
            return false;
        }
        return Pattern.matches(REGEX_USERNAME, username);
    }
 
    /**
     * 校验密码
     *
     * @param password
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isPassword(String password) {
        if (StringUtil.isEmpty(password)) {
            return false;
        }
        return Pattern.matches(REGEX_PASSWORD, password);
    }
 
    /**
     * 校验手机号
     *
     * @param mobile
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isMobile(String mobile) {
        if (StringUtil.isEmpty(mobile)) {
            return false;
        }
        String telValidate = MappingCache.getValue("SYSTEM_SWITCH","TEL_VALIDATE");
        if("OFF".equals(telValidate)){
            return true;
        }
        return Pattern.matches(REGEX_MOBILE, mobile);
    }
 
    /**
     * 校验邮箱
     *
     * @param email
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isEmail(String email) {
        if (StringUtil.isEmpty(email)) {
            return false;
        }
        return Pattern.matches(REGEX_EMAIL, email);
    }
 
    /**
     * 校验汉字
     *
     * @param chinese
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isChinese(String chinese) {
        if (StringUtil.isEmpty(chinese)) {
            return false;
        }
        return Pattern.matches(REGEX_CHINESE, chinese);
    }
 
    /**
     * 校验身份证
     *
     * @param idCard
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isIDCard(String idCard) {
        if (StringUtil.isEmpty(idCard)) {
            return false;
        }
        return Pattern.matches(REGEX_ID_CARD, idCard);
    }
 
    /**
     * 校验URL
     *
     * @param url
     * @return 校验通过返回true,否则返回false
     */
    public static boolean isUrl(String url) {
        if (StringUtil.isEmpty(url)) {
            return false;
        }
        return Pattern.matches(REGEX_URL, url);
    }
 
    /**
     * 校验IP地址
     *
     * @param ipAddr
     * @return
     */
    public static boolean isIPAddr(String ipAddr) {
        if (StringUtil.isEmpty(ipAddr)) {
            return false;
        }
        return Pattern.matches(REGEX_IP_ADDR, ipAddr);
    }
 
}