iOS之判断手机号码、邮箱格式是否正确

 1 //判断手机号码格式是否正确
 2 + (BOOL)valiMobile:(NSString *)mobile{
 3     mobile = [mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
 4     if (mobile.length != 11)
 5     {
 6         return NO;
 7     }else{
 8         /**
 9          * 移动号段正则表达式
10          */
11         NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
12         /**
13          * 联通号段正则表达式
14          */
15         NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
16         /**
17          * 电信号段正则表达式
18          */
19         NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
20         NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];
21         BOOL isMatch1 = [pred1 evaluateWithObject:mobile];
22         NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];
23         BOOL isMatch2 = [pred2 evaluateWithObject:mobile];
24         NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];
25         BOOL isMatch3 = [pred3 evaluateWithObject:mobile];
26  
27         if (isMatch1 || isMatch2 || isMatch3) {
28             return YES;
29         }else{
30             return NO;
31         }
32     }
33 }
34  
35 //判断邮箱格式是否正确
36 //利用正则表达式验证
37 + (BOOL)isAvailableEmail:(NSString *)email {
38     NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
39     NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
40     return [emailTest evaluateWithObject:email];
41 }

 

posted @ 2016-11-21 10:57  Jaycee麦子  阅读(1288)  评论(0编辑  收藏  举报