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;
}
posted @ 2018-09-18 17:56  明似水  阅读(436)  评论(0编辑  收藏  举报