chenjiahe
2024-05-09 45579f96ef266fa9000dd22dbefa5825c6f500b1
提交 | 用户 | 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}$");
5c5945 15     
E 16     /**
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
c42df9 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;
5c5945 104     }
E 105
106     /**
107     * 验证输入身份证号
108     * 
109     * @param str 待验证的字符�?
110     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
111     */
112     public static boolean IsIDcard(String str) {
113         String regex = "(^\\d{18}$)|(^\\d{15}$)";
114         return match(regex, str);
115     }
116
117     /**
118     * 验证输入两位小数
119     * 
120     * @param str 待验证的字符�?
121     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
122     */
123     public static boolean IsDecimal(String str) {
124         String regex = "^[0-9]+(.[0-9]{2})?$";
125         return match(regex, str);
126     }
127
128     /**
129     * 验证输入�?年的12个月
130     * 
131     * @param str 待验证的字符�?
132     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
133     */
134     public static boolean IsMonth(String str) {
135         String regex = "^(0?[[1-9]|1[0-2])$";
136         return match(regex, str);
137     }
138
139     /**
140     * 验证输入�?个月�?31�?
141     * 
142     * @param str 待验证的字符�?
143     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
144     */
145     public static boolean IsDay(String str) {
146         String regex = "^((0?[1-9])|((1|2)[0-9])|30|31)$";
147         return match(regex, str);
148     }
149
150     /**
151     * 验证日期时间
152     * 
153     * @param str 待验证的字符�?
154     * @return 如果是符合网�?格式的字符串,返回 <b>true </b>,否则�? <b>false </b>
155     */
156     public static boolean isDate(String str) {
157         // 严格验证时间格式�?(匹配[2002-01-31], [1997-04-30],
158         // [2004-01-01])不匹�?([2002-01-32], [2003-02-29], [04-01-01])
159         // String regex =
160         // "^((((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)))))$";
161         // 没加时间验证的YYYY-MM-DD
162         // String regex =
163         // "^((((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-))$";
164         // 加了时间验证的YYYY-MM-DD 00:00:00
165         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$";
166         return match(regex, str);
167     }
168
169     /**
170     * 验证数字输入
171     * 
172     * @param str 待验证的字符�?
173     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
174     */
175     public static boolean IsNumber(String str) {
176         String regex = "^[0-9]*$";
177         return match(regex, str);
178     }
179
180     /**
181     * 验证非零的正整数
182     * 
183     * @param str 待验证的字符�?
184     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
185     */
186     public static boolean IsIntNumber(String str) {
187         String regex = "^[1-9][0-9]*$";
188         return match(regex, str);
189     }
190
191     /**
192     * 验证大写字母
193     * 
194     * @param str 待验证的字符�?
195     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
196     */
197     public static boolean IsUpChar(String str) {
198         String regex = "^[A-Z]+$";
199         return match(regex, str);
200     }
201
202     /**
203     * 验证小写字母
204     * 
205     * @param str 待验证的字符�?
206     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
207     */
208     public static boolean IsLowChar(String str) {
209         String regex = "^[a-z]+$";
210         return match(regex, str);
211     }
212
213     /**
214     * 验证验证输入字母
215     * 
216     * @param str 待验证的字符�?
217     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
218     */
219     public static boolean IsLetter(String str) {
220         String regex = "^[A-Za-z]+$";
221         return match(regex, str);
222     }
223
224     /**
225     * 验证验证输入汉字
226     * 
227     * @param str 待验证的字符�?
228     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
229     */
230     public static boolean IsChinese(String str) {
231         String regex = "^[\u4e00-\u9fa5],{0,}$";
232         return match(regex, str);
233     }
234
235     /**
236     * 验证验证输入字符�?
237     * 
238     * @param str 待验证的字符�?
239     * @return 如果是符合格式的字符�?,返回 <b>true </b>,否则�? <b>false </b>
240     */
241     public static boolean IsLength(String str) {
242         String regex = "^.{8,}$";
243         return match(regex, str);
244     }
245
246     /**
0e1429 247      * 判断字符串是不是double型
E 248      * @param str
249      * @return
250      */
251     public static boolean isNumeric(String str){
252         Pattern pattern = Pattern.compile("[0-9]+[.]{0,1}[0-9]*[dD]{0,1}");
253         Matcher isNum = pattern.matcher(str);
254         if( !isNum.matches() ){
255             return false;
256         }
257         return true;
258     }
259
260     /**
5c5945 261     * @param regex
E 262     * 正则表达式字符串
263     * @param str
264     * 要匹配的字符�?
265     * @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;
266     */
267     private static boolean match(String regex, String str) {
268         Pattern pattern = Pattern.compile(regex);
269         Matcher matcher = pattern.matcher(str);
270         return matcher.matches();
271     }
272 }