2016-08-29 14:36:44 u012352203 阅读数 329

前端时间没事要做一个登录功能,有一个要进行验证手机号码功能。利用iOS Predicate 即谓词逻辑。和数据库的SQL语句具有相似性,都是从数据堆中根据条件进行筛选。


+ (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;
}

2016-10-20 16:39:32 koocui 阅读数 543

旧版正则检测:

- (BOOL)isMobileNumber:(NSString *)mobileNum
{
    /**
     * 手机号码
     * 移动:134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188
     * 联通:130,131,132,145,152,155,156,1709,171,176,185,186
     * 电信:133,134,153,1700,177,180,181,189
     */
    NSString * MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|7[01678]|8[0-9])\\\\d{8}$";
    /**
     * 中国移动:China Mobile
     * 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188
     */
    NSString * CM = @"^1(3[4-9]|4[7]|5[0-27-9]|7[0]|7[8]|8[2-478])\\\\d{8}$";
    /**
     * 中国联通:China Unicom
     * 130,131,132,145,152,155,156,1709,171,176,185,186
     */
    NSString * CU = @"^1(3[0-2]|4[5]|5[56]|709|7[1]|7[6]|8[56])\\\\d{8}$";
    /**
     * 中国电信:China Telecom
     * 133,134,153,1700,177,180,181,189
     */
    NSString * CT = @"^1(3[34]|53|77|700|8[019])\\\\d{8}$";
    /**
     * 大陆地区固话及小灵通
     * 区号:010,020,021,022,023,024,025,027,028,029
     * 号码:七位或八位
     */
    // 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];

    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

新版正则检测

+ (BOOL)isMobileNumber:(NSString *)mobileNum
{
    if (mobileNum.length != 11)
    {
        return NO;
    }
    /**
     * 手机号码: 
     * 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[0, 1, 6, 7, 8], 18[0-9]
     * 移动号段: 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188
     * 联通号段: 130,131,132,145,152,155,156,170,171,176,185,186
     * 电信号段: 133,134,153,170,177,180,181,189
     */
    NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|7[01678]|8[0-9])\\\\d{8}$";
    /**
     * 中国移动:China Mobile
     * 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188
     */
    NSString *CM = @"^1(3[4-9]|4[7]|5[0-27-9]|7[08]|8[2-478])\\\\d{8}$";
    /**
     * 中国联通:China Unicom
     * 130,131,132,145,152,155,156,170,171,176,185,186
     */
    NSString *CU = @"^1(3[0-2]|4[5]|5[256]|7[016]|8[56])\\\\d{8}$";
    /**
     * 中国电信:China Telecom
     * 133,134,153,170,177,180,181,189
     */
    NSString *CT = @"^1(3[34]|53|7[07]|8[019])\\\\d{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];

    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

简易版新的


+ (BOOL)validateMobile:(NSString *)mobileNum
{
    //手机号以13, 15,18, 14, 17开头,八个 \d 数字字符
    NSString *phoneRegex = @“^1(3[0-9])”

@"^1(3[0-9]|4[57]|5[0-35-9]|7[01678]|8[0-9])\\\\d{8}$";

    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];

    return [phoneTest evaluateWithObject:mobileNum];


}



#pragma mark—————验证手机号码—————


+ (BOOL)validateMobile:(NSString *)mobileNum
{
    //手机号以13, 15,18, 14, 17开头,八个 \d 数字字符
    NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9])|(14[0-9])|(17[0-9]))\\d{8}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];

    return [phoneTest evaluateWithObject:mobileNum];


}
2017-10-13 10:29:39 FlyingKuiKui 阅读数 1749
///检测手机号码的合法性
BOOL isValidMobile(NSString * _Nullable mobile){
   if (mobile.length != 11){
        return NO;
     }
    /**
     * 手机号码:
     * 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[0, 1, 6, 7, 8], 18[0-9]
     * 移动号段: 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188
     * 联通号段: 130,131,132,145,155,156,170,171,175,176,185,186
     * 电信号段: 133,149,153,170,173,177,180,181,189
     */
    NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|7[0135678]|8[0-9])\\d{8}$";

    // 移动 134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188
    NSString *CM = @"^1(3[4-9]|4[7]|5[0-27-9]|7[08]|8[2-478])\\d{8}$";
    
    // 联通 130,131,132,145,155,156,170,171,175,176,185,186
    NSString *CU = @"^1(3[0-2]|4[5]|5[56]|7[0156]|8[56])\\d{8}$";
    // 电信 133,149,153,170,173,177,180,181,189
    NSString *CT = @"^1(3[3]|4[9]|53|7[037]|8[019])\\d{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];
    if (([regextestmobile evaluateWithObject:mobile] == YES) ||
        ([regextestcm evaluateWithObject:mobile] == YES) ||
        ([regextestct evaluateWithObject:mobile] == YES) ||
        ([regextestcu evaluateWithObject:mobile] == YES)){
            return YES;
    }else{
        return NO;
    }
}
2015-04-22 22:16:33 ljx286803517 阅读数 1962

1.根据IOS SDK提供的接口无法在应用关闭的情况下检测到SIM卡的状态改变,只有在应用开启状态下才可以检测到。

          以下是苹果文档提供的信息:

          Use the CTTelephonyNetworkInfo class to respond to changes in the user’s cellular service provider. This occurs, for example, if a  user swaps the device’s SIM card with one from another provider, while your application is running.


2.根据IOS SDK提供的接口无法获取手机号码,如果使用私有API获取手机号码会被苹果商店拒绝。

       以下是因为获取手机号被苹果商店拒绝的理由:

       "For security reasons, iPhone OS restricts an application (including its preferences and data) to a unique location in the file system. This restriction is part of the security feature known as the application's "sandbox." The sandbox is a set of fine-grained controls limiting an application's access to files, preferences, network resources, hardware, and so on."


The device's phone number is not available within your application's container. You will need to revise your application to read only within your directory container and resubmit your binary to iTunes Connect in order for your application to be reconsidered for the App Store.

2015-01-27 16:46:58 smile_qin 阅读数 543

- (BOOL)isMobileNumber:(NSString*)mobileNum

{

   NSString * MOBILE = @"^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[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 = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", MOBILE];

   NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELFMATCHES %@", CM];

   NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELFMATCHES %@", CU];

   NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELFMATCHES %@", CT];

   if (([regextestmobile evaluateWithObject:mobileNum] == YES)

       || ([regextestcm evaluateWithObject:mobileNum] == YES)

       || ([regextestct evaluateWithObject:mobileNum] == YES)

       || ([regextestcu evaluateWithObject:mobileNum] == YES))

    {

       return YES;

    }

   else

    {

       return NO;

    }

}


没有更多推荐了,返回首页