判断手机号

来源:互联网 发布:linux启动oracle监听 编辑:程序博客网 时间:2024/04/25 18:17

在iOS开发中经常会去判断手机号码,以下有几种方法,其实质都是相同的,只是正规表达式的写法不同而已。


方法一:

-(BOOL)checkPhoneNumInput:(NSString *)mobile{    if (mobile.length !=11||mobile.length!=14)return NO;    if (mobile.length==11){           NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9]|70|77)\\d{8}$";           NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";           NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";           NSString * CT = @"^1((33|53|8[09])[0-9]|349)\\d{7}$";           // 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];               BOOL res1 = [regextestmobile evaluateWithObject:mobile];        BOOL res2 = [regextestcm evaluateWithObject:mobile];        BOOL res3 = [regextestcu evaluateWithObject:mobile];        BOOL res4 = [regextestct evaluateWithObject:mobile];        if (res1 || res2 || res3 || res4 )        {            return YES;        }else{            return NO;        }    }else if(mobile.length==14){        NSString * MOBILE = @"^[+861](3[0-9]|5[0-35-9]|8[025-9]|70|77)\\d{8}$";        NSString * CM = @"^[+861](34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$";        NSString * CU = @"^[+861](3[0-2]|5[256]|8[56])\\d{8}$";        NSString * CT = @"^[+861]((33|53|8[09])[0-9]|349)\\d{7}$";        // 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];        BOOL res1 = [regextestmobile evaluateWithObject:mobile];        BOOL res2 = [regextestcm evaluateWithObject:mobile];        BOOL res3 = [regextestcu evaluateWithObject:mobile];        BOOL res4 = [regextestct evaluateWithObject:mobile];        if (res1 || res2 || res3 || res4 )        {            return YES;        }else{            return NO;        }    }   }

方法二

//不正确的话会返回 请输入正确的手机号码+ (NSString *)valiMobile:(NSString *)mobile{    if (mobile.length < 11)    {        return @"手机号长度只能是11位";    }else{        /**         * 移动号段正则表达式         */        NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";        /**         * 联通号段正则表达式         */        NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";        /**         * 电信号段正则表达式         */        NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";        NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];        BOOL isMatch1 = [pred1 evaluateWithObject:mobile];        NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];        BOOL isMatch2 = [pred2 evaluateWithObject:mobile];        NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];        BOOL isMatch3 = [pred3 evaluateWithObject:mobile];        if (isMatch1 || isMatch2 || isMatch3) {            return nil;        }else{            return @"请输入正确的手机号码";        }    }    return nil;}

如果直接写成扩展,那就可以装逼了。

0 0
原创粉丝点击