iOS正则表达式

来源:互联网 发布:rpc端口 编辑:程序博客网 时间:2024/05/17 03:18
//正则表达式,判断手机号码格式是否正确- (BOOL)checkTel:(NSString *)str{    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;}//正则判断邮箱格式是否正确- (BOOL)validateEmail:(NSString *)email{    NSLog(@"3333333");    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];    BOOL isMatch = [emailTest evaluateWithObject:email];    if (!isMatch) {        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确的邮箱" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];        [alert show];        return NO;    }    return YES;}
0 0
原创粉丝点击