代码:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.title=@"验证邮箱的合法性"; NSString *email=@"123456@qq.com"; 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-