iOS基础(十一)——验证电子邮箱

分享一下我验证电子邮箱的方法

+ (BOOL) validateEmail:(NSString *)email
{
    if (email.length == 0) {
        NSString *message = @"邮箱不能为空!";
        [app showToastView:message];
        return NO;
    }
    
    NSString *expression = [NSString stringWithFormat:@"^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$"];
    NSError *error = NULL;
    NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:expression
                                                                      options:NSRegularExpressionCaseInsensitive
                                                                        error:&error];
    NSTextCheckingResult *match = [regex firstMatchInString:email
                                                    options:0
                                                      range:NSMakeRange(0,[email length])];
    if (!match) {
       [app showToastView:@"邮箱格式不对"];
        return NO;
    }
    return YES;
}

猜你喜欢

转载自www.cnblogs.com/smileK/p/9670445.html