Xtr*_*ian 2 regex email iphone objective-c nsregularexpression
我在联系人屏幕上有一个文本字段,用户需要输入电子邮件地址给我发消息.什么是确保用户输入有效电子邮件地址的最佳方式,例如:
a@b.com / net / org / co.il
abc@gmail.com
abc@yahoo.com
Run Code Online (Sandbox Code Playgroud)
等等..
谢谢
请尝试以下方法:
- (BOOL) validateEmail: (NSString *) candidate {
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
// return 0;
return [emailTest evaluateWithObject:candidate];
}
-(IBAction)btnTapped:(id)sender{
if([self validateEmail:[txtEmail text]] ==1)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"You Enter Correct Email id." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
[alert release];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"You Enter Incoorect Email id." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
[alert release];
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1501 次 |
| 最近记录: |