[iOS]验证手机号及拨打电话

验证手机号及拨打电话

/// 拨打电话
+ (void)callTelpromptWithPhone:(NSString *)phone {
    NSString *tempPhone = [NSString stringWithFormat:@"telprompt://%@",phone];
    if (@available(iOS 10.0, *)) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:tempPhone] options:@{} completionHandler:nil];
    } else {
        // Fallback on earlier versions
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:tempPhone]];
    }
}
/// 替换手机号码中间四位为*号
+ (NSString *)replacingSubStringInPhone:(NSString *)thePhone {
    NSString *newString = thePhone;
    if (thePhone.length > 8) {
        // 字符串的截取
        NSString *string = [thePhone substringWithRange:NSMakeRange(3,4)];
        // 字符串的替换
        newString = [thePhone stringByReplacingOccurrencesOfString:string withString:@"****"];
    }
    return newString;
}
/// 验证手机号码规范
+ (BOOL)checkMobileNumber:(NSString *)moblieNum {
    /**
     * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
     * 联通:130,131,132,152,155,156,185,186
     * 电信:133,1349,153,180,189,181,173,174,177
     */
    NSString * MOBIL = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
    NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[2378])\\d)\\d{7}$";
    NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
    NSString * CT = @"^1((33|53|7[347]|8[019])[0-9]|349)\\d{7}$";
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBIL];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:moblieNum]
         || [regextestcm evaluateWithObject:moblieNum]
         || [regextestct evaluateWithObject:moblieNum]
         || [regextestcu evaluateWithObject:moblieNum])) {
        // 手机号可用
        return YES;
    } else {
        return NO;
    }
}

猜你喜欢

转载自blog.csdn.net/u012881779/article/details/80857834