1 #import "NSString+RegexCategory.h"
2
3 @implementation NSString (RegexCategory)
4 #pragma mark - 正则相关
5 - (BOOL)isValidateByRegex:(NSString *)regex{
6 NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
7 return [pre evaluateWithObject:self];
8 }
9
10 #pragma mark -
11
12 //手机号分服务商
13 - (BOOL)isMobileNumberClassification{
14 /**
15 * 手机号码
16 * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188,1705
17 * 联通:130,131,132,152,155,156,185,186,1709
18 * 电信:133,1349,153,180,189,1700
19 */
20 // NSString * MOBILE = @"^1((3//d|5[0-35-9]|8[025-9])//d|70[059])\\d{7}$";//总况
21
22 /**
23 10 * 中国移动:China Mobile
24 11 * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188,1705
25 12 */
26 NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d|705)\\d{7}$";
27 /**
28 15 * 中国联通:China Unicom
29 16 * 130,131,132,152,155,156,185,186,1709
30 17 */
31 NSString * CU = @"^1((3[0-2]|5[256]|8[56])\\d|709)\\d{7}$";
32 /**
33 20 * 中国电信:China Telecom
34 21 * 133,1349,153,180,189,1700
35 22 */
36 NSString * CT = @"^1((33|53|8[09])\\d|349|700)\\d{7}$";
37
38
39 /**
40 25 * 大陆地区固话及小灵通
41 26 * 区号:010,020,021,022,023,024,025,027,028,029
42 27 * 号码:七位或八位
43 28 */
44 NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
45
46
47 // NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
48
49 if (([self isValidateByRegex:CM])
50 || ([self isValidateByRegex:CU])
51 || ([self isValidateByRegex:CT])
52 || ([self isValidateByRegex:PHS]))
53 {
54 return YES;
55 }
56 else
57 {
58 return NO;
59 }
60 }
61
62 //手机号有效性
63 - (BOOL)isMobileNumber{
64 /**
65 * 手机号以13、15、18、170开头,8个 \d 数字字符
66 * 小灵通 区号:010,020,021,022,023,024,025,027,028,029 还有未设置的新区号xxx
67 */
68 NSString *mobileNoRegex = @"^1((3\\d|5[0-35-9]|8[025-9])\\d|70[059])\\d{7}$";//除4以外的所有个位整数,不能使用[^4,\\d]匹配,这里是否iOS Bug?
69 NSString *phsRegex =@"^0(10|2[0-57-9]|\\d{3})\\d{7,8}$";
70
71 BOOL ret = [self isValidateByRegex:mobileNoRegex];
72 BOOL ret1 = [self isValidateByRegex:phsRegex];
73
74 return (ret || ret1);
75 }
76
77 //邮箱
78 - (BOOL)isEmailAddress{
79 NSString *emailRegex = @"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
80 return [self isValidateByRegex:emailRegex];
81 }
82
83 //身份证号
84 - (BOOL) simpleVerifyIdentityCardNum
85 {
86 NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
87 return [self isValidateByRegex:regex2];
88 }
89
90 //车牌
91 - (BOOL)isCarNumber{
92 //车牌号:湘K-DE829 香港车牌号码:粤Z-J499港
93 NSString *carRegex = @"^[\u4e00-\u9fff]{1}[a-zA-Z]{1}[-][a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fff]$";//其中\u4e00-\u9fa5表示unicode编码中汉字已编码部分,\u9fa5-\u9fff是保留部分,将来可能会添加
94 return [self isValidateByRegex:carRegex];
95 }
96
97 - (BOOL)isMacAddress{
98 NSString * macAddRegex = @"([A-Fa-f\\d]{2}:){5}[A-Fa-f\\d]{2}";
99 return [self isValidateByRegex:macAddRegex];
100 }
101
102 - (BOOL)isValidUrl
103 {
104 NSString *regex = @"^((http)|(https))+:[^\\s]+\\.[^\\s]*$";
105 return [self isValidateByRegex:regex];
106 }
107
108 - (BOOL)isValidChinese;
109 {
110 NSString *chineseRegex = @"^[\u4e00-\u9fa5]+$";
111 return [self isValidateByRegex:chineseRegex];
112 }
113
114 - (BOOL)isValidPostalcode {
115 NSString *postalRegex = @"^[0-8]\\d{5}(?!\\d)$";
116 return [self isValidateByRegex:postalRegex];
117 }
118
119 - (BOOL)isValidTaxNo
120 {
121 NSString *taxNoRegex = @"[0-9]\\d{13}([0-9]|X)$";
122 return [self isValidateByRegex:taxNoRegex];
123 }
124
125 - (BOOL)isValidWithMinLenth:(NSInteger)minLenth
126 maxLenth:(NSInteger)maxLenth
127 containChinese:(BOOL)containChinese
128 firstCannotBeDigtal:(BOOL)firstCannotBeDigtal;
129 {
130 // [\u4e00-\u9fa5A-Za-z0-9_]{4,20}
131 NSString *hanzi = containChinese ? @"\u4e00-\u9fa5" : @"";
132 NSString *first = firstCannotBeDigtal ? @"^[a-zA-Z_]" : @"";
133
134 NSString *regex = [NSString stringWithFormat:@"%@[%@A-Za-z0-9_]{%d,%d}", first, hanzi, (int)(minLenth-1), (int)(maxLenth-1)];
135 return [self isValidateByRegex:regex];
136 }
137
138 - (BOOL)isValidWithMinLenth:(NSInteger)minLenth
139 maxLenth:(NSInteger)maxLenth
140 containChinese:(BOOL)containChinese
141 containDigtal:(BOOL)containDigtal
142 containLetter:(BOOL)containLetter
143 containOtherCharacter:(NSString *)containOtherCharacter
144 firstCannotBeDigtal:(BOOL)firstCannotBeDigtal;
145 {
146 NSString *hanzi = containChinese ? @"\u4e00-\u9fa5" : @"";
147 NSString *first = firstCannotBeDigtal ? @"^[a-zA-Z_]" : @"";
148 NSString *lengthRegex = [NSString stringWithFormat:@"(?=^.{%@,%@}$)", @(minLenth), @(maxLenth)];
149 NSString *digtalRegex = containDigtal ? @"(?=(.*\\d.*){1})" : @"";
150 NSString *letterRegex = containLetter ? @"(?=(.*[a-zA-Z].*){1})" : @"";
151 NSString *characterRegex = [NSString stringWithFormat:@"(?:%@[%@A-Za-z0-9%@]+)", first, hanzi, containOtherCharacter ? containOtherCharacter : @""];
152 NSString *regex = [NSString stringWithFormat:@"%@%@%@%@", lengthRegex, digtalRegex, letterRegex, characterRegex];
153 return [self isValidateByRegex:regex];
154 }
155
156 #pragma mark - 算法相关
157 //精确的身份证号码有效性检测
158 + (BOOL)accurateVerifyIDCardNumber:(NSString *)value {
159 value = [value stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
160
161 int length =0;
162 if (!value) {
163 return NO;
164 }else {
165 length = (int)value.length;
166
167 if (length !=15 && length !=18) {
168 return NO;
169 }
170 }
171 // 省份代码
172 NSArray *areasArray =@[@"11",@"12", @"13",@"14", @"15",@"21", @"22",@"23", @"31",@"32", @"33",@"34", @"35",@"36", @"37",@"41", @"42",@"43", @"44",@"45", @"46",@"50", @"51",@"52", @"53",@"54", @"61",@"62", @"63",@"64", @"65",@"71", @"81",@"82", @"91"];
173
174 NSString *valueStart2 = [value substringToIndex:2];
175 BOOL areaFlag =NO;
176 for (NSString *areaCode in areasArray) {
177 if ([areaCode isEqualToString:valueStart2]) {
178 areaFlag =YES;
179 break;
180 }
181 }
182
183 if (!areaFlag) {
184 return false;
185 }
186
187
188 NSRegularExpression *regularExpression;
189 NSUInteger numberofMatch;
190
191 int year =0;
192 switch (length) {
193 case 15:
194 year = [value substringWithRange:NSMakeRange(6,2)].intValue +1900;
195
196 if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) {
197
198 regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}$"
199 options:NSRegularExpressionCaseInsensitive
200 error:nil];//测试出生日期的合法性
201 }else {
202 regularExpression = [[NSRegularExpression alloc]initWithPattern:@"^[1-9][0-9]{5}[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}$"
203 options:NSRegularExpressionCaseInsensitive
204 error:nil];//测试出生日期的合法性
205 }
206 numberofMatch = [regularExpression numberOfMatchesInString:value
207 options:NSMatchingReportProgress
208 range:NSMakeRange(0, value.length)];
209
210 if(numberofMatch >0) {
211 return YES;
212 }else {
213 return NO;
214 }
215 case 18:
216 year = [value substringWithRange:NSMakeRange(6,4)].intValue;
217 if (year %4 ==0 || (year %100 ==0 && year %4 ==0)) {
218
219 regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|[1-2][0-9]))[0-9]{3}[0-9Xx]$"
220 options:NSRegularExpressionCaseInsensitive
221 error:nil];//测试出生日期的合法性
222 }else {
223 regularExpression = [[NSRegularExpression alloc] initWithPattern:@"^[1-9][0-9]{5}19[0-9]{2}((01|03|05|07|08|10|12)(0[1-9]|[1-2][0-9]|3[0-1])|(04|06|09|11)(0[1-9]|[1-2][0-9]|30)|02(0[1-9]|1[0-9]|2[0-8]))[0-9]{3}[0-9Xx]$"
224 options:NSRegularExpressionCaseInsensitive
225 error:nil];//测试出生日期的合法性
226 }
227 numberofMatch = [regularExpression numberOfMatchesInString:value
228 options:NSMatchingReportProgress
229 range:NSMakeRange(0, value.length)];
230
231 if(numberofMatch >0) {
232 int S = ([value substringWithRange:NSMakeRange(0,1)].intValue + [value substringWithRange:NSMakeRange(10,1)].intValue) *7 + ([value substringWithRange:NSMakeRange(1,1)].intValue + [value substringWithRange:NSMakeRange(11,1)].intValue) *9 + ([value substringWithRange:NSMakeRange(2,1)].intValue + [value substringWithRange:NSMakeRange(12,1)].intValue) *10 + ([value substringWithRange:NSMakeRange(3,1)].intValue + [value substringWithRange:NSMakeRange(13,1)].intValue) *5 + ([value substringWithRange:NSMakeRange(4,1)].intValue + [value substringWithRange:NSMakeRange(14,1)].intValue) *8 + ([value substringWithRange:NSMakeRange(5,1)].intValue + [value substringWithRange:NSMakeRange(15,1)].intValue) *4 + ([value substringWithRange:NSMakeRange(6,1)].intValue + [value substringWithRange:NSMakeRange(16,1)].intValue) *2 + [value substringWithRange:NSMakeRange(7,1)].intValue *1 + [value substringWithRange:NSMakeRange(8,1)].intValue *6 + [value substringWithRange:NSMakeRange(9,1)].intValue *3;
233 int Y = S %11;
234 NSString *M =@"F";
235 NSString *JYM =@"10X98765432";
236 M = [JYM substringWithRange:NSMakeRange(Y,1)];// 判断校验位
237 if ([M isEqualToString:[value substringWithRange:NSMakeRange(17,1)]]) {
238 return YES;// 检测ID的校验位
239 }else {
240 return NO;
241 }
242
243 }else {
244 return NO;
245 }
246 default:
247 return NO;
248 }
249 }
250
251
252
253 /** 银行卡号有效性问题Luhn算法
254 * 现行 16 位银联卡现行卡号开头 6 位是 622126~622925 之间的,7 到 15 位是银行自定义的,
255 * 可能是发卡分行,发卡网点,发卡序号,第 16 位是校验码。
256 * 16 位卡号校验位采用 Luhm 校验方法计算:
257 * 1,将未带校验位的 15 位卡号从右依次编号 1 到 15,位于奇数位号上的数字乘以 2
258 * 2,将奇位乘积的个十位全部相加,再加上所有偶数位上的数字
259 * 3,将加法和加上校验位能被 10 整除。
260 */
261 - (BOOL)bankCardluhmCheck{
262 NSString * lastNum = [[self substringFromIndex:(self.length-1)] copy];//取出最后一位
263 NSString * forwardNum = [[self substringToIndex:(self.length -1)] copy];//前15或18位
264
265 NSMutableArray * forwardArr = [[NSMutableArray alloc] initWithCapacity:0];
266 for (int i=0; i<forwardNum.length; i++) {
267 NSString * subStr = [forwardNum substringWithRange:NSMakeRange(i, 1)];
268 [forwardArr addObject:subStr];
269 }
270
271 NSMutableArray * forwardDescArr = [[NSMutableArray alloc] initWithCapacity:0];
272 for (int i = (int)(forwardArr.count-1); i> -1; i--) {//前15位或者前18位倒序存进数组
273 [forwardDescArr addObject:forwardArr[i]];
274 }
275
276 NSMutableArray * arrOddNum = [[NSMutableArray alloc] initWithCapacity:0];//奇数位*2的积 < 9
277 NSMutableArray * arrOddNum2 = [[NSMutableArray alloc] initWithCapacity:0];//奇数位*2的积 > 9
278 NSMutableArray * arrEvenNum = [[NSMutableArray alloc] initWithCapacity:0];//偶数位数组
279
280 for (int i=0; i< forwardDescArr.count; i++) {
281 NSInteger num = [forwardDescArr[i] intValue];
282 if (i%2) {//偶数位
283 [arrEvenNum addObject:[NSNumber numberWithInteger:num]];
284 }else{//奇数位
285 if (num * 2 < 9) {
286 [arrOddNum addObject:[NSNumber numberWithInteger:num * 2]];
287 }else{
288 NSInteger decadeNum = (num * 2) / 10;
289 NSInteger unitNum = (num * 2) % 10;
290 [arrOddNum2 addObject:[NSNumber numberWithInteger:unitNum]];
291 [arrOddNum2 addObject:[NSNumber numberWithInteger:decadeNum]];
292 }
293 }
294 }
295
296 __block NSInteger sumOddNumTotal = 0;
297 [arrOddNum enumerateObjectsUsingBlock:^(NSNumber * obj, NSUInteger idx, BOOL *stop) {
298 sumOddNumTotal += [obj integerValue];
299 }];
300
301 __block NSInteger sumOddNum2Total = 0;
302 [arrOddNum2 enumerateObjectsUsingBlock:^(NSNumber * obj, NSUInteger idx, BOOL *stop) {
303 sumOddNum2Total += [obj integerValue];
304 }];
305
306 __block NSInteger sumEvenNumTotal =0 ;
307 [arrEvenNum enumerateObjectsUsingBlock:^(NSNumber * obj, NSUInteger idx, BOOL *stop) {
308 sumEvenNumTotal += [obj integerValue];
309 }];
310
311 NSInteger lastNumber = [lastNum integerValue];
312
313 NSInteger luhmTotal = lastNumber + sumEvenNumTotal + sumOddNum2Total + sumOddNumTotal;
314
315 return (luhmTotal%10 ==0)?YES:NO;
316 }
317
318 - (BOOL)isIPAddress{
319 NSString *regex = [NSString stringWithFormat:@"^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})$"];
320 NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
321 BOOL rc = [pre evaluateWithObject:self];
322
323 if (rc) {
324 NSArray *componds = [self componentsSeparatedByString:@","];
325
326 BOOL v = YES;
327 for (NSString *s in componds) {
328 if (s.integerValue > 255) {
329 v = NO;
330 break;
331 }
332 }
333
334 return v;
335 }
336
337 return NO;
338 }
339
340
341
342 @end