-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathVerificationUtils.java
More file actions
207 lines (183 loc) · 7.28 KB
/
VerificationUtils.java
File metadata and controls
207 lines (183 loc) · 7.28 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
package com.realmo.utils;
import android.text.TextUtils;
import java.text.SimpleDateFormat;
import java.util.regex.Pattern;
public class VerificationUtils {
public static boolean matcherRealName(String value) {
String regex = "^([\\u4e00-\\u9fa5]+|([a-zA-Z]+\\s?)+)$";
return testRegex(regex, value);
}
public static boolean matcherPhoneNum(String value) {
String regex = "^(\\+?\\d{2}-?)?(1[0-9])\\d{9}$";
return testRegex(regex, value);
}
public static boolean matcherAccount(String value) {
String regex = "[\\u4e00-\\u9fa5a-zA-Z0-9\\-]{4,20}";
return testRegex(regex, value);
}
public static boolean matcherPassword(String value) {
String regex = "^[a-zA-Z0-9]{6,12}$";
return testRegex(regex, value);
}
public static boolean matcherPassword2(String value) {
String regex = "(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,}";
return testRegex(regex, value);
}
public static boolean matcherEmail(String value) {
// String regex = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)" +
// "+[a-zA-Z]{2,}$";
String regex = "^[a-z0-9!#$%&'*+/=?^_`{|}~-]+" +
"(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+" +
"[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$";
return testRegex(regex, value);
}
public static boolean matcherIP(String value) {
String regex = "\\b((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\" +
"d\\d|2[0-4]\\d|25[0-5])\\.((?!\\d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\.((?!\\" +
"d\\d\\d)\\d+|1\\d\\d|2[0-4]\\d|25[0-5])\\b";
return testRegex(regex, value.toLowerCase());
}
public static boolean matcherUrl(String value) {
//String regex = "^(([hH][tT]{2}[pP][sS]?)|([fF][tT][pP]))\\:\\/\\/[wW]{3}\\.[\\w-]+\\.\\w{2,4}(\\/.*)?$";
String regex = "^(([hH][tT]{2}[pP][sS]?)|([fF][tT][pP]))\\:\\/\\/[\\w-]+\\.\\w{2,4}(\\/.*)?$";
return testRegex(regex, value.toLowerCase());
}
public static boolean matcherVehicleNumber(String value) {
String regex = "^[京津晋冀蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼川贵云藏陕甘青宁新渝]?[A-Z][A-HJ-NP-Z0-9学挂港澳练]{5}$";
return testRegex(regex, value.toLowerCase());
}
public static boolean matcherIdentityCard(String value) {
// String regex = "^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|" +
// "(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|" +
// "\\d{3}[Xx])$)$";
// return testRegex(regex, value);
IDCardTester idCardTester = new IDCardTester();
return idCardTester.test(value);
}
private static class IDCardTester {
public boolean test(String content) {
if (TextUtils.isEmpty(content)) {
return false;
}
final int length = content.length();
if (15 == length) {
try {
return isOldCNIDCard(content);
} catch (NumberFormatException e) {
e.printStackTrace();
return false;
}
} else if (18 == length) {
return isNewCNIDCard(content);
} else {
return false;
}
}
final int[] WEIGHT = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
final char[] VALID = {'1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'};
public boolean isNewCNIDCard(String numbers) {
numbers = numbers.toUpperCase();
int sum = 0;
for (int i = 0; i < WEIGHT.length; i++) {
final int cell = Character.getNumericValue(numbers.charAt(i));
sum += WEIGHT[i] * cell;
}
int index = sum % 11;
return VALID[index] == numbers.charAt(17);
}
public boolean isOldCNIDCard(String numbers) {
String yymmdd = numbers.substring(6, 11);
boolean aPass = numbers.equals(String.valueOf(Long.parseLong(numbers)));
boolean yPass = true;
try {
new SimpleDateFormat("yyMMdd").parse(yymmdd);
} catch (Exception e) {
e.printStackTrace();
yPass = false;
}
return aPass && yPass;
}
}
public static boolean isNumeric(String input) {
if (TextUtils.isEmpty(input)) {
return false;
}
char[] chars = input.toCharArray();
int sz = chars.length;
boolean hasExp = false;
boolean hasDecPoint = false;
boolean allowSigns = false;
boolean foundDigit = false;
int start = (chars[0] == '-' || chars[0] == '+') ? 1 : 0;
if (sz > start + 1) {
if (chars[start] == '0' && chars[start + 1] == 'x') {
int i = start + 2;
if (i == sz) {
return false;
}
for (; i < chars.length; i++) {
if ((chars[i] < '0' || chars[i] > '9')
&& (chars[i] < 'a' || chars[i] > 'f')
&& (chars[i] < 'A' || chars[i] > 'F')) {
return false;
}
}
return true;
}
}
sz--;
int i = start;
while (i < sz || (i < sz + 1 && allowSigns && !foundDigit)) {
if (chars[i] >= '0' && chars[i] <= '9') {
foundDigit = true;
allowSigns = false;
} else if (chars[i] == '.') {
if (hasDecPoint || hasExp) {
return false;
}
hasDecPoint = true;
} else if (chars[i] == 'e' || chars[i] == 'E') {
if (hasExp) {
return false;
}
if (!foundDigit) {
return false;
}
hasExp = true;
allowSigns = true;
} else if (chars[i] == '+' || chars[i] == '-') {
if (!allowSigns) {
return false;
}
allowSigns = false;
foundDigit = false;
} else {
return false;
}
i++;
}
if (i < chars.length) {
if (chars[i] >= '0' && chars[i] <= '9') {
return true;
}
if (chars[i] == 'e' || chars[i] == 'E') {
return false;
}
if (!allowSigns && (chars[i] == 'd' || chars[i] == 'D' || chars[i] == 'f' || chars[i] == 'F')) {
return foundDigit;
}
if (chars[i] == 'l' || chars[i] == 'L') {
return foundDigit && !hasExp;
}
return false;
}
return !allowSigns && foundDigit;
}
public static boolean testRegex(String regex, String inputValue) {
return Pattern.compile(regex).matcher(inputValue).matches();
}
public static boolean checkPostcode(String postcode) {
String regex = "[1-9]\\d{5}";
return Pattern.matches(regex, postcode);
}
}