cmg
2024-05-27 8e79771fd46206cdb6fb698f5dc78dc41886238f
提交 | 用户 | age
5c5945 1 package com.hx.util;
E 2
3 import java.util.regex.Matcher;
4 import java.util.regex.Pattern;
5
6 /**
7  * 正则表达式验证工�?
8  * @author mgchen
9  *
10  */
11 public class RegValidatorUtil {
45579f 12
C 13     /**手机号正则初始化*/
14     public static Pattern pattern = Pattern.compile("^1[3-9]\\d{9}$");
8e7977 15
5c5945 16     /**
E 17     * 验证邮箱
18     * 
19     * @param str 待验证的字符
20     * @return 如果是符合的字符�?,返回 <b>true </b>,否则�? <b>false </b>
21     */
22     public static boolean isEmail(String str) {
23         String regex = "^([\\w-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([\\w-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
24         return match(regex, str);
25     }
26
27     /**
28     * 验证IP地址
29     * 
30     * @param str 待验证的字符�?
31     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
32     */
33     public static boolean isIP(String str) {
34         String num = "(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)";
35         String regex = "^" + num + "\\." + num + "\\." + num + "\\." + num + "$";
36         return match(regex, str);
37     }
38
39     /**
40     * 验证网址Url
41     * 
42     * @param str 待验证的字符�?
43     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
44     */
45     public static boolean IsUrl(String str) {
46         String regex = "http(s)?://([\\w-]+\\.)+[\\w-]+(/[\\w- ./?%&=]*)?";
47         return match(regex, str);
48     }
49
50     /**
51     * 验证电话号码
52     * 
53     * @param str 待验证的字符�?
54     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
55     */
56     public static boolean IsTelephone(String str) {
57         String regex = "^(\\d{3,4}-)?\\d{6,8}$";
58         return match(regex, str);
59     }
60
61     /**
62     * 验证输入密码条件(字符与数据同时出�?)
63     * 
64     * @param str 待验证的字符�?
65     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
66     */
67     public static boolean IsPassword(String str) {
68         String regex = "[A-Za-z0-9]+";
69         return match(regex, str);
70     }
71
72     /**
73     * 验证输入密码长度 (6-18�?)
74     * 
75     * @param str 待验证的字符�?
76     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
77     */
78     public static boolean IsPasswLength(String str) {
79         String regex = "^\\d{6,18}$";
80         return match(regex, str);
81     }
82
83     /**
84     * 验证输入邮政编号
85     * 
86     * @param str 待验证的字符�?
87     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
88     */
89     public static boolean IsPostalcode(String str) {
90         String regex = "^\\d{6}$";
91         return match(regex, str);
92     }
93
cbc905 94     /**校验手机号码
C 95      * @param phoneNumber 手机号
96      * @return 校验结果
97      */
98     public static boolean IsHandset(String phoneNumber) {
99         if ((phoneNumber != null) && (!phoneNumber.isEmpty())) {
45579f 100             Matcher m = pattern.matcher(phoneNumber);
C 101             return m.matches();
c42df9 102         }
C 103         return false;
8e7977 104     /**校验手机号码
C 105      * @param phoneNumber 手机号
106      * @return 校验结果
107      */
108     public static boolean IsHandset(String phoneNumber) {
109         if ((phoneNumber != null) && (!phoneNumber.isEmpty())) {
cbc905 110             return Pattern.matches("^1[3-9]\\d{9}$", phoneNumber);
C 111         }
112         return false;
5c5945 113     }
E 114
115     /**
116     * 验证输入身份证号
117     * 
118     * @param str 待验证的字符�?
119     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
120     */
121     public static boolean IsIDcard(String str) {
122         String regex = "(^\\d{18}$)|(^\\d{15}$)";
123         return match(regex, str);
124     }
125
126     /**
127     * 验证输入两位小数
128     * 
129     * @param str 待验证的字符�?
130     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
131     */
132     public static boolean IsDecimal(String str) {
133         String regex = "^[0-9]+(.[0-9]{2})?$";
134         return match(regex, str);
135     }
136
137     /**
138     * 验证输入�?年的12个月
139     * 
140     * @param str 待验证的字符�?
141     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
142     */
143     public static boolean IsMonth(String str) {
144         String regex = "^(0?[[1-9]|1[0-2])$";
145         return match(regex, str);
146     }
147
148     /**
149     * 验证输入�?个月�?31�?
150     * 
151     * @param str 待验证的字符�?
152     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
153     */
154     public static boolean IsDay(String str) {
155         String regex = "^((0?[1-9])|((1|2)[0-9])|30|31)$";
156         return match(regex, str);
157     }
158
159     /**
160     * 验证日期时间
161     * 
162     * @param str 待验证的字符�?
163     * @return 如果是符合网�?格式的字符串,返回 <b>true </b>,否则�? <b>false </b>
164     */
165     public static boolean isDate(String str) {
166         // 严格验证时间格式�?(匹配[2002-01-31], [1997-04-30],
167         // [2004-01-01])不匹�?([2002-01-32], [2003-02-29], [04-01-01])
168         // String regex =
169         // "^((((19|20)(([02468][048])|([13579][26]))-02-29))|((20[0-9][0-9])|(19[0-9][0-9]))-((((0[1-9])|(1[0-2]))-((0[1-9])|(1\\d)|(2[0-8])))|((((0[13578])|(1[02]))-31)|(((01,3-9])|(1[0-2]))-(29|30)))))$";
170         // 没加时间验证的YYYY-MM-DD
171         // String regex =
172         // "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$";
173         // 加了时间验证的YYYY-MM-DD 00:00:00
174         String regex = "^((((1[6-9]|[2-9]\\d)\\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\\d|3[01]))|(((1[6-9]|[2-9]\\d)\\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\\d|30))|(((1[6-9]|[2-9]\\d)\\d{2})-0?2-(0?[1-9]|1\\d|2[0-8]))|(((1[6-9]|[2-9]\\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-)) (20|21|22|23|[0-1]?\\d):[0-5]?\\d:[0-5]?\\d$";
175         return match(regex, str);
176     }
177
178     /**
179     * 验证数字输入
180     * 
181     * @param str 待验证的字符�?
182     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
183     */
184     public static boolean IsNumber(String str) {
185         String regex = "^[0-9]*$";
186         return match(regex, str);
187     }
188
189     /**
190     * 验证非零的正整数
191     * 
192     * @param str 待验证的字符�?
193     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
194     */
195     public static boolean IsIntNumber(String str) {
196         String regex = "^[1-9][0-9]*$";
197         return match(regex, str);
198     }
199
200     /**
201     * 验证大写字母
202     * 
203     * @param str 待验证的字符�?
204     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
205     */
206     public static boolean IsUpChar(String str) {
207         String regex = "^[A-Z]+$";
208         return match(regex, str);
209     }
210
211     /**
212     * 验证小写字母
213     * 
214     * @param str 待验证的字符�?
215     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
216     */
217     public static boolean IsLowChar(String str) {
218         String regex = "^[a-z]+$";
219         return match(regex, str);
220     }
221
222     /**
223     * 验证验证输入字母
224     * 
225     * @param str 待验证的字符�?
226     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
227     */
228     public static boolean IsLetter(String str) {
229         String regex = "^[A-Za-z]+$";
230         return match(regex, str);
231     }
232
233     /**
234     * 验证验证输入汉字
235     * 
236     * @param str 待验证的字符�?
237     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
238     */
239     public static boolean IsChinese(String str) {
240         String regex = "^[\u4e00-\u9fa5],{0,}$";
241         return match(regex, str);
242     }
243
244     /**
245     * 验证验证输入字符�?
246     * 
247     * @param str 待验证的字符�?
248     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
249     */
250     public static boolean IsLength(String str) {
251         String regex = "^.{8,}$";
252         return match(regex, str);
253     }
254
255     /**
0e1429 256      * 判断字符串是不是double型
E 257      * @param str
258      * @return
259      */
260     public static boolean isNumeric(String str){
261         Pattern pattern = Pattern.compile("[0-9]+[.]{0,1}[0-9]*[dD]{0,1}");
262         Matcher isNum = pattern.matcher(str);
263         if( !isNum.matches() ){
264             return false;
265         }
266         return true;
267     }
268
269     /**
5c5945 270     * @param regex
E 271     * 正则表达式字符串
272     * @param str
273     * 要匹配的字符�?
274     * @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;
275     */
276     private static boolean match(String regex, String str) {
277         Pattern pattern = Pattern.compile(regex);
278         Matcher matcher = pattern.matcher(str);
279         return matcher.matches();
280     }
281 }