iOS 身份证,邮箱,手机号验证

 1 //邮箱验证
 2 + (BOOL) validateEmail:(NSString *)email
 3 {
 4     NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
 5     NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
 6     return [emailTest evaluateWithObject:email];
 7 }
 8 
 9 //身份证号
10 + (BOOL) validateIdentityCard: (NSString *)identityCard
11 {
12     BOOL flag;
13     if (identityCard.length <= 0) {
14         flag = NO;
15         return flag;
16     }
17     NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
18     NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
19     return [identityCardPredicate evaluateWithObject:identityCard];
20 }
21 
22 //数字验证
23 + (BOOL)validateNumber:(NSString*)number
24 {
25     BOOL res = YES;
26     NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
27     int i = 0;
28     while (i < number.length) {
29         NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
30         NSRange range = [string rangeOfCharacterFromSet:tmpSet];
31         if (range.length == 0) {
32             res = NO;
33             break;
34         }
35         i++;
36     }
37     return res;
38 }
39 /**
40  *  正则表达式验证手机号
41  *
42  *  @param mobile 传入手机号
43  *
44  *  @return
45  */
46 - (BOOL)validateMobile:(NSString *)mobile
47 {
48     // 130-139  150-153,155-159  180-189  145,147  170,171,173,176,177,178
49     NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0-9])|(14[57])|(17[013678]))\\d{8}$";
50     NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
51     return [phoneTest evaluateWithObject:mobile];
52 }

 

posted @ 2017-02-13 15:35  福泽小院  阅读(192)  评论(2编辑  收藏  举报