验证邮箱的合法性

验证邮箱的合法性

IOS开发中怎样验证邮箱的合法性

文章参考:http://www.codes51.com/article/detail_94157.html

代码:

- (void)viewDidLoad
{
    [super viewDidLoad]; // Do any additional setup after loading the view.  self.title=@"验证邮箱的合法性";

    NSString *email=@"[email protected]";
    NSLog(@"--%i-",[self isValidateEmail:email]);

}

 

//用正则表达式 -(BOOL)isValidateEmail:(NSString *)email {

    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";

    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex]; return [emailTest evaluateWithObject:email];

}

输出:

2014-11-17 14:29:54.704 验证邮箱的合法性[29052:60b] --1-

05-02 20:26