JLChen
2021-04-30 a5247b61d585627a1a7b1e1f35f34de9f0af9fba
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
//
//  Copyright © 2019 dahua. All rights reserved.
//
 
#import "NSString+Verify.h"
#import "LCProgressHUD.h"
 
#define EMOJI          @"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]"
#define SPACE          @"[//s//p{Zs}]"
#define LC_ILLEGALCHAR @"[•€`~!#$%^&*+=|{}()':;',\\[\\]<>/?~!#¥%⋯⋯&*()+|{}【】‘;:\"”“’。,、?]"
#define LC_NEMECHAR @"[0-9a-zA-Z\u4e00-\u9fa5\\@\\_\\-\\ ]+"
 
 
 
@implementation NSString (Verify)
 
//检查空串
- (BOOL)isNull {
    if ([self isEqualToString:@""]) {
        return YES;
    }
    return NO;
}
 
//检查有效手机号
- (BOOL)isVaildPhone {
    /**
     * 手机号码:
     * 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 16[6], 17[5, 6, 7, 8], 18[0-9], 170[0-9], 19[89]
     * 移动号段: 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705,198
     * 联通号段: 130,131,132,155,156,185,186,145,175,176,1709,166
     * 电信号段: 133,153,180,181,189,177,1700,199
     */
    NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|6[6]|7[05-8]|8[0-9]|9[89])\\d{8}$";
 
    NSString *CM = @"(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478]|9[8])\\d{8}$)|(^1705\\d{7}$)";
 
    NSString *CU = @"(^1(3[0-2]|4[5]|5[56]|66|7[56]|8[56])\\d{8}$)|(^1709\\d{7}$)";
 
    NSString *CT = @"(^1(33|53|77|73|8[019]|99)\\d{8}$)|(^1700\\d{7}$)";
 
    /**
     * 大陆地区固话及小灵通
     * 区号:010,020,021,022,023,024,025,027,028,029
     * 号码:七位或八位
     */
    // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
 
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    // NSPredicate *regextestPHS = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", PHS];
 
    if (([regextestmobile evaluateWithObject:self] == YES)
        || ([regextestcm evaluateWithObject:self] == YES)
        || ([regextestct evaluateWithObject:self] == YES)
        || ([regextestcu evaluateWithObject:self] == YES)) {
        return YES;
    } else {
        return NO;
    }
}
 
- (NSString *)vaildDeviceName {
    NSString *vaildStr = @"";
    vaildStr = [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    vaildStr = [self disableSpecialCharInString:vaildStr];
    if (vaildStr.length > 20) {
        vaildStr = [vaildStr substringToIndex:20];
    }
    return vaildStr;
}
 
//过滤emoji
- (NSString *)disableEmojiInString:(NSString *)text {
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:EMOJI options:NSRegularExpressionCaseInsensitive error:nil];
    NSString *modifiedString = [regex stringByReplacingMatchesInString:text
                                                               options:0
                                                                 range:NSMakeRange(0, [text length])
                                                          withTemplate:@""];
    return modifiedString;
}
 
//过滤空白符
- (NSString *)disableSpaceInString:(NSString *)text {
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:SPACE options:NSRegularExpressionCaseInsensitive error:nil];
    NSString *modifiedString = [regex stringByReplacingMatchesInString:text
                                                               options:0
                                                                 range:NSMakeRange(0, [text length])
                                                          withTemplate:@""];
    return modifiedString;
}
 
//过滤特殊符号
- (NSString *)disableSpecialCharInString:(NSString *)text {
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:LC_ILLEGALCHAR options:NSRegularExpressionCaseInsensitive error:nil];
    NSString *modifiedString = [regex stringByReplacingMatchesInString:text
                                                               options:0
                                                                 range:NSMakeRange(0, [text length])
                                                          withTemplate:@""];
    return modifiedString;
}
 
//根据正则,过滤特殊字符
- (NSString *)filterCharactor:(NSString *)string withRegex:(NSString *)regexStr{
    NSString *searchText = string;
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr options:NSRegularExpressionCaseInsensitive error:&error];
    NSString *result = [regex stringByReplacingMatchesInString:searchText options:NSMatchingReportCompletion range:NSMakeRange(0, searchText.length) withTemplate:@""];
    return result;
}
 
-(BOOL)isCharactor{
    NSString *regex = @"[^\u4e00-\u9fa5]";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL result = [predicate evaluateWithObject:self];
    return result;
}
 
-(BOOL)isVaildDeviceName{
    NSString *regex = LC_NEMECHAR;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL result = [predicate evaluateWithObject:self];
    return result;
}
 
- (BOOL)isSafeCode {
    NSString *regex = @"^[A-Za-z0-9]{5,7}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL result = [predicate evaluateWithObject:self];
    return result;
}
 
- (BOOL)isVaildEmail {
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    BOOL result = [emailTest evaluateWithObject:self];
    return result;
}
 
- (BOOL)isVaildSNCode {
    if (self.length < 10 || self.length > 32) {
        return NO;
    }
    NSString *regex = @"^[A-Za-z0-9]{10,32}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL result = [predicate evaluateWithObject:self];
    return result;
}
 
- (BOOL)isVaildOverseaSNCode {
    return YES;
}
 
- (BOOL)isFullNumber {
    NSString *regex = @"^[0-9]+$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL result = [predicate evaluateWithObject:self];
    return result;
}
 
- (BOOL)isFullChar {
    NSString *regex = @"^[A-Za-z]+$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL result = [predicate evaluateWithObject:self];
    return result;
}
 
- (BOOL)isVaildPasswordBit {
    return [self lc_pwdVerifiers];
}
 
- (BOOL)isExistBlankSpace {
    NSString *regex = @"^[^ ]+$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL result = [predicate evaluateWithObject:self];
    return result;
}
 
- (BOOL)isVaildURL {
    NSString *regex = @"((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)";
 
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    BOOL result = [predicate evaluateWithObject:self];
    return result;
}
 
- (BOOL)lc_pwdVerifiers {
    if (self.length < 8 || self.length > 32) {
        [LCProgressHUD showMsg:@"device_manager_encryption_password_rule".lc_T];
        return false;
    }
    int numOfType = [self passwordStrength:self];
    if (numOfType < 2) {
        [LCProgressHUD showMsg:@"add_device_password_too_simple".lc_T];
        return false;
    }
    //判断密码是否合法
    if ([self checkInvalidPassword:self]) {
        [LCProgressHUD showMsg:@"add_device_password_too_simple".lc_T];
        return false;
    }
    if ([self checkSameCharacter:self]) {
        [LCProgressHUD showMsg:@"add_device_password_too_simple".lc_T];
        return false;
    }
    if ([self checkSameCharacter:self]) {
        [LCProgressHUD showMsg:@"add_device_password_too_simple".lc_T];
        return false;
    }
    return true;
}
 
- (int)passwordStrength:(NSString *)pasword
{
    int nRet = 0;//默认密码强度
    //   NSString *regex = @"[a-z][A-Z][0-9]";
    NSString *smallChar = @"abcdefghijklmnopqrstuvwxyz";//小写字母
    NSString *bigChar = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";//大写字母
    NSString *numberChar = @"1234567890";//数字
    NSString *otherChar = @" -/:;()$&@\".,?!'[]{}#%^*+=_\\|~<>.,?!'";//特殊字符
    NSString *urlString = pasword;
 
    //    int nIndex = 0;
    NSString *strType[4] = { smallChar, bigChar, numberChar, otherChar };
    for (int j = 0; j < 4; j++) {//4种字符类型:小写字母,大写字母、数字、特殊字符(仅限ASCii码)
        for (int i = 0; i < [urlString length]; i++) {
            NSRange r;
            r.length = 1;
            r.location = i;
            NSString *c = [urlString substringWithRange:r];
            //NSString* serialChar = @"1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            if ([strType[j] rangeOfString:c].location != NSNotFound) {
                nRet++;
                break;
            }
        }
    }
    return nRet;
}
 
- (BOOL)checkInvalidPassword:(NSString *)password {
    if (password.length > 0) {
        if ([[self initializeInvalidPassword] containsObject:password]) {
            return YES;
        }
    }
    return NO;
}
 
- (NSArray *)initializeInvalidPassword {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"InvalidPasswordList" ofType:@"plist"];
    NSArray *list = [[NSArray alloc] initWithContentsOfFile:filePath];
    return list;
}
 
- (BOOL)checkSameCharacter:(NSString *)password {
    if (password.length > 0) {
        NSString *checkerString = @"^.*(.)\\1{5}.*$";
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", checkerString];
        return [predicate evaluateWithObject:password];
    }
    return NO;
}
 
@end