iOS--判断一个字符串是不是手机号

//判断手机号是否正确

- (BOOL)checkTel:(NSString *)str

 

{

    if ([str length] == 0) {

        

        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"手机号不能为空" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

        

        [alert show];

        return NO;

    }

    

    NSString *regex = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";

    

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

    

    BOOL isMatch = [pred evaluateWithObject:str];

    

    if (!isMatch) {

        

        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确的手机号码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

        

        [alert show];

        return NO;

    }

    return YES;

}

 

posted @ 2014-12-15 18:22  Colaless  阅读(1435)  评论(0编辑  收藏  举报