ValidateUtils.java
19.3 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
package com.bjivt.util;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.*;
/**
*
* @description:验证工具类
* @author JASON
* @date 2016-2-17
*
*/
public class ValidateUtils {
/** 整数 */
private static final String V_INTEGER="^-?[1-9]\\d*$";
/** 正整数 */
private static final String V_Z_INDEX="^[1-9]\\d*$";
/**负整数 */
private static final String V_NEGATIVE_INTEGER="^-[1-9]\\d*$";
/** 数字 */
private static final String V_NUMBER="^([+-]?)\\d*\\.?\\d+$";
/**正数 */
private static final String V_POSITIVE_NUMBER="^[1-9]\\d*|0$";
/** 负数 */
private static final String V_NEGATINE_NUMBER="^-[1-9]\\d*|0$";
/** 浮点数 */
private static final String V_FLOAT="^([+-]?)\\d*\\.\\d+$";
/** 正浮点数 */
private static final String V_POSTTIVE_FLOAT="^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*$";
/** 负浮点数 */
private static final String V_NEGATIVE_FLOAT="^-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*)$";
/** 非负浮点数(正浮点数 + 0) */
private static final String V_UNPOSITIVE_FLOAT="^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*|0?.0+|0$";
/** 非正浮点数(负浮点数 + 0) */
private static final String V_UN_NEGATIVE_FLOAT="^(-([1-9]\\d*.\\d*|0.\\d*[1-9]\\d*))|0?.0+|0$";
/** 邮件 */
private static final String V_EMAIL="^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";
/** 颜色 */
private static final String V_COLOR="^[a-fA-F0-9]{6}$";
/** url */
private static final String V_URL="^http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-./?%&=]*)?$";
/** 仅中文 */
private static final String V_CHINESE="^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$";
/** 仅ACSII字符 */
private static final String V_ASCII="^[\\x00-\\xFF]+$";
/** 邮编 */
private static final String V_ZIPCODE="^\\d{6}$";
/** 手机 */
private static final String V_MOBILE="^((13[0-9][0-9])|14[5,7][0-9]|(15[^4,\\D][0-9])|(17[6-8][0-9])|(170[0,5,9])|(18[0-9][0-9]))\\d{7}$";
/** ip地址 */
private static final String V_IP4="^(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)$";
/** 非空 */
private static final String V_NOTEMPTY="^\\S+$";
/** 图片 */
private static final String V_PICTURE="(.*)\\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$";
/** 压缩文件 */
private static final String V_RAR="(.*)\\.(rar|zip|7zip|tgz)$";
/** 日期 */
private static final String V_DATE="^((((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$";
/** QQ号码 */
private static final String V_QQ_NUMBER="^[1-9]*[1-9][0-9]*$";
/** 电话号码的函数(包括验证国内区号,国际区号,分机号) */
private static final String V_TEL="^(([0\\+]\\d{2,3}-)?(0\\d{2,3})-)?(\\d{7,8})(-(\\d{3,}))?$";
/** 用来用户注册。匹配由数字、26个英文字母或者下划线组成的字符串 */
private static final String V_USERNAME="^\\w+$";
/** 字母 */
private static final String V_LETTER="^[A-Za-z]+$";
/** 大写字母 */
private static final String V_LETTER_U="^[A-Z]+$";
/** 小写字母 */
private static final String V_LETTER_I="^[a-z]+$";
/** 身份证 */
private static final String V_IDCARD ="^(\\d{15}$|^\\d{18}$|^\\d{17}(\\d|X|x))$";
/**验证密码(数字和英文同时存在)*/
private static final String V_PASSWORD_REG="[A-Za-z]+[0-9]";
/**验证密码长度(6-18位)*/
private static final String V_PASSWORD_LENGTH="^\\d{6,18}$";
/**验证两位数*/
private static final String V_TWO_POINT="^[0-9]+(.[0-9]{2})?$";
/**验证一个月的31天*/
private static final String V_31DAYS="^((0?[1-9])|((1|2)[0-9])|30|31)$";
public static Map<String, String> vallidateMap= new HashMap<String, String>(){{
put("V_INTEGER", "验证是不是整数");
put("V_Z_INDEX", "验证是不是正整数");
put("V_NEGATIVE_INTEGER", "验证是不是负整数");
put("V_NUMBER", "验证是不是数字");
put("V_POSITIVE_NUMBER", "验证是不是正数");
put("V_NEGATINE_NUMBER", "验证是不是负数");
put("V_31DAYS", "验证一个月的31天");
put("V_ASCII", "验证是不是ASCII");
put("V_CHINESE", "验证是不是中文");
put("V_COLOR", "验证是不是颜色");
put("V_DATE", "验证是不是日期");
put("V_EMAIL", "验证是不是邮箱地址");
put("V_FLOAT", "验证是不是浮点数");
put("V_IDCARD", "验证是不是正确的身份证号码");
put("V_IP4", "验证是不是正确的IP地址");
put("V_LETTER", "验证是不是字母");
put("V_LETTER_I", "验证是不是小写字母");
put("V_LETTER_U", "验证是不是大写字母");
put("V_MOBILE", "验证是不是手机号码");
put("V_NEGATIVE_FLOAT", "验证是不是负浮点数");
put("V_NOTEMPTY", "验证非空");
put("V_PASSWORD_LENGTH", "验证密码的长度(6~18位)");
put("V_PASSWORD_REG", "验证密码(数字和英文同时存在)");
put("V_PICTURE", "验证图片");
put("V_POSTTIVE_FLOAT", "验证正浮点数");
put("V_QQ_NUMBER", "验证QQ号码");
put("V_RAR", "验证压缩文件");
put("V_TEL", "验证电话");
put("V_TWO_POINT", "验证两位小数");
put("V_UN_NEGATIVE_FLOAT", "验证非正浮点数");
put("V_UNPOSITIVE_FLOAT", "验证非负浮点数");
put("V_URL", "验证URL");
put("V_USERNAME", "验证用户注册。匹配由数字、26个英文字母或者下划线组成的字符串");
put("V_ZIPCODE", "验证邮编");
}};
public static Map<String, String> zh_msg= new HashMap<String, String>(){{
put("V_INTEGER", "请输入整数");
put("V_Z_INDEX", "请输入正整数");
put("V_NEGATIVE_INTEGER", "请输入负整数");
put("V_NUMBER", "请输入数字");
put("V_POSITIVE_NUMBER", "请输入正数");
put("V_NEGATINE_NUMBER", "请输入负数");
put("V_31DAYS", "请输入正确的天");
put("V_ASCII", "请输入ASCII");
put("V_CHINESE", "请输入正确的中文");
put("V_COLOR", "请输入正确颜色");
put("V_DATE", "请输入正确的日期");
put("V_EMAIL", "请输入正确的邮箱地址");
put("V_FLOAT", "请输入正确浮点数");
put("V_IDCARD", "请输入正确身份证号");
put("V_IP4", "请输入正确的IP地址");
put("V_LETTER", "请输入字母");
put("V_LETTER_I", "请输入小写字母");
put("V_LETTER_U", "请输入大写字母");
put("V_MOBILE", "请输入正确手机号码");
put("V_NEGATIVE_FLOAT", "请输入浮点数");
put("V_NOTEMPTY", "不能为空");
put("V_PASSWORD_LENGTH", "密码的长度(6~18位)");
put("V_PASSWORD_REG", "密码(数字和英文同时存在)");
put("V_PICTURE", "验证图片");
put("V_POSTTIVE_FLOAT", "验证正浮点数");
put("V_QQ_NUMBER", "验证QQ号码");
put("V_RAR", "验证压缩文件");
put("V_TEL", "验证电话");
put("V_TWO_POINT", "验证两位小数");
put("V_UN_NEGATIVE_FLOAT", "验证非正浮点数");
put("V_UNPOSITIVE_FLOAT", "验证非负浮点数");
put("V_URL", "验证URL");
put("V_USERNAME", "验证用户注册。匹配由数字、26个英文字母或者下划线组成的字符串");
put("V_ZIPCODE", "请输入正确邮编");
}};
private ValidateUtils(){}
public static boolean Validate(String type,String value){
switch (type) {
/** 整数 */
case "V_INTEGER":
return Integer(value);
/** 验证是不是正整数*/
case "V_Z_INDEX":
return Z_index(value);
case "V_NEGATIVE_INTEGER":
return Negative_integer(value);
case "V_NUMBER":
return Number(value);
case "V_POSITIVE_NUMBER":
return PositiveNumber(value);
case "V_NEGATINE_NUMBER":
return NegatineNumber(value);
case "V_31DAYS":
return Is31Days(value);
case "V_ASCII":
return ASCII(value);
case "V_CHINESE":
return Chinese(value);
case "V_COLOR":
return Color(value);
case "V_DATE":
return Date(value);
case "V_EMAIL":
return Email(value);
case "V_FLOAT":
return Float(value);
case "V_IDCARD":
return IDcard(value);
case "V_IP4":
return IP4(value);
case "V_LETTER":
return Letter(value);
case "V_LETTER_I":
return Letter_i(value);
case "V_LETTER_U":
return Letter_u(value);
case "V_MOBILE":
return Mobile(value);
case "V_NEGATIVE_FLOAT":
return Negative_float(value);
case "V_NOTEMPTY":
return Notempty(value);
case "V_PASSWORD_LENGTH":
return Number_length(value);
case "V_PASSWORD_REG":
return Password_reg(value);
case "V_PICTURE":
return Picture(value);
case "V_POSTTIVE_FLOAT":
return Posttive_float(value);
case "V_QQ_NUMBER":
return QQnumber(value);
case "V_RAR":
return Rar(value);
case "V_TEL":
return Tel(value);
case "V_TWO_POINT":
return Two_point(value);
case "V_UN_NEGATIVE_FLOAT":
return Un_negative_float(value);
case "V_UNPOSITIVE_FLOAT":
return Unpositive_float(value);
case "V_URL":
return Url(value);
case "V_USERNAME":
return UserName(value);
case "V_ZIPCODE":
return Zipcode(value);
default:
return false;
}
}
/**
* 验证是不是整数
* @param value 要验证的字符串 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean Integer(String value){
return match(V_INTEGER,value);
}
/**
* 验证是不是正整数
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean Z_index(String value){
return match(V_Z_INDEX,value);
}
/**
* 验证是不是负整数
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean Negative_integer(String value){
return match(V_NEGATIVE_INTEGER,value);
}
/**
* 验证是不是数字
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean Number(String value){
return match(V_NUMBER,value);
}
/**
* 验证是不是正数
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean PositiveNumber(String value){
return match(V_POSITIVE_NUMBER,value);
}
/**
* 验证是不是负数
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean NegatineNumber(String value){
return match(V_NEGATINE_NUMBER,value);
}
/**
* 验证一个月的31天
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean Is31Days(String value){
return match(V_31DAYS,value);
}
/**
* 验证是不是ASCII
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean ASCII(String value){
return match(V_ASCII,value);
}
/**
* 验证是不是中文
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean Chinese(String value){
return match(V_CHINESE,value);
}
/**
* 验证是不是颜色
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean Color(String value){
return match(V_COLOR,value);
}
/**
* 验证是不是日期
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean Date(String value){
return match(V_DATE,value);
}
/**
* 验证是不是邮箱地址
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean Email(String value){
return match(V_EMAIL,value);
}
/**
* 验证是不是浮点数
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean Float(String value){
return match(V_FLOAT,value);
}
/**
* 验证是不是正确的身份证号码
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IDcard(String value){
return match(V_IDCARD,value);
}
/**
* 验证是不是正确的IP地址
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean IP4(String value){
return match(V_IP4,value);
}
/**
* 验证是不是字母
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean Letter(String value){
return match(V_LETTER,value);
}
/**
* 验证是不是小写字母
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean Letter_i(String value){
return match(V_LETTER_I,value);
}
/**
* 验证是不是大写字母
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean Letter_u(String value){
return match(V_LETTER_U,value);
}
/**
* 验证是不是手机号码
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean Mobile(String value){
return match(V_MOBILE,value);
}
/**
* 验证是不是负浮点数
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean Negative_float(String value){
return match(V_NEGATIVE_FLOAT,value);
}
/**
* 验证非空
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean Notempty(String value){
return match(V_NOTEMPTY,value);
}
/**
* 验证密码的长度(6~18位)
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean Number_length(String value){
return match(V_PASSWORD_LENGTH,value);
}
/**
* 验证密码(数字和英文同时存在)
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean Password_reg(String value){
return match(V_PASSWORD_REG,value);
}
/**
* 验证图片
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean Picture(String value){
return match(V_PICTURE,value);
}
/**
* 验证正浮点数
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean Posttive_float(String value){
return match(V_POSTTIVE_FLOAT,value);
}
/**
* 验证QQ号码
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean QQnumber(String value){
return match(V_QQ_NUMBER,value);
}
/**
* 验证压缩文件
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean Rar(String value){
return match(V_RAR,value);
}
/**
* 验证电话
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean Tel(String value){
return match(V_TEL,value);
}
/**
* 验证两位小数
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean Two_point(String value){
return match(V_TWO_POINT,value);
}
/**
* 验证非正浮点数
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean Un_negative_float(String value){
return match(V_UN_NEGATIVE_FLOAT,value);
}
/**
* 验证非负浮点数
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean Unpositive_float(String value){
return match(V_UNPOSITIVE_FLOAT,value);
}
/**
* 验证URL
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean Url(String value){
return match(V_URL,value);
}
/**
* 验证用户注册。匹配由数字、26个英文字母或者下划线组成的字符串
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean UserName(String value){
return match(V_USERNAME,value);
}
/**
* 验证邮编
* @param value 要验证的字符串
* @return 如果是符合格式的字符串,返回 <b>true </b>,否则为 <b>false </b>
*/
public static boolean Zipcode(String value){
return match(V_ZIPCODE,value);
}
/**
* @param regex 正则表达式字符串
* @param str 要匹配的字符串
* @return 如果str 符合 regex的正则表达式格式,返回true, 否则返回 false;
*/
private static boolean match(String regex, String str)
{
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
return matcher.matches();
}
}