代码:
- (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._%+-][email protected][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-
时间: 2024-10-07 11:07:05