iOS最新手机号码正则表达

基础方法

/**
 基础方法

 @param regex 正则表达式
 @return 正则验证成功返回YES, 否则返回NO
 */
- (BOOL)isValidateByRegex:(NSString *)regex {
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [predicate evaluateWithObject:self];
}

一、验证手机号码 不区别运营商

/**
 验证手机号码

 @return 正则验证成功返回YES, 否则返回NO
 */
- (BOOL)isMobileNumber {// @"^(13[0-9]|14[56789]|15[0-9]|16[6]|17[0-9]|18[0-9]|19[189])\\d{8}$";
    NSString *emailRegex = @"^1(3[0-9]|4[56789]|5[0-9]|6[6]|7[0-9]|8[0-9]|9[189])\\d{8}$";
    return [self isValidateByRegex:emailRegex];
}

二、验证手机号码 区别运营商

可点击查看哟

各运营商号段

/**
 判断运营商
 各个运营商开头号码不同 需要根据运营商更新

 @return  正则验证成功返回YES, 否则返回NO
 */
- (BOOL)isMobileNumberClassification {
    /**
     * 手机号码
     * 移动:134(0-8)、135、136、137、138、139、147、150、151、152、157、158、159、172、178、182、183、184、187、188、198
     *
     * 联通:130、131、132、145、155、156、166、175、176、185、186
     *
     * 电信:133、149、153、173、177、180、181、189、191、199
     *
        虚拟运营商
        电信:1700、1701、1702
        移动:1703、1705、1706
        联通:1704、1707、1708、1709、171
        卫星通信:1349
     */
    /**
                * 中国移动:China Mobile
                * 134(0-8)、135、136、137、138、139、147、150、151、152、157、158、159、172、178、182、183、184、187、188、198
     
     */
    NSString * CM = @"^1(34[0-8]|3[5-9]|47|5[0127-9]|8[23478]|98)\\d{8}$";
    /**
                * 中国联通:China Unicom
                * 130、131、132、145、155、156、166、175、176、185、186
     */
    NSString * CU = @"^1((3[0-2]|45|5[56]|166|7[56]|8[56]))\\d{8}$";
    /**
                * 中国电信:China Telecom
                * 133、149、153、173、177、180、181、189、191、199
     */
    NSString * CT = @"^1((33|49|53|7[37]|8[019]|9[19]))\\d{8}$";
   
    if ([self isValidateByRegex:CM]) {
        NSLog(@"手机运营商是====CM---中国移动");
        return YES;
    } else if ([self isValidateByRegex:CU]) {
        NSLog(@"手机运营商是====CU---中国联通");
        return YES;
    } else if ([self isValidateByRegex:CT]){
        NSLog(@"手机运营商是====CT---中国电信");
        return YES;
    } else {
        return NO;
    }
    
}

以上!!!
有错误 欢迎指出!谢谢大家!!!

你可能感兴趣的:(iOS最新手机号码正则表达)