iphone - 确保用户输入的电子邮件地址字符串格式正确?

标签 iphone objective-c regex email nsregularexpression

我在“联系人”屏幕中有一个文本字段,用户需要输入电子邮件地址才能向我发送消息。 确保用户输入有效电子邮件地址的最佳方法是什么,例如:

a@b.com / net / org / co.il
abc@gmail.com
abc@yahoo.com

等等..

谢谢

最佳答案

尝试以下操作:

- (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];
    }
}

关于iphone - 确保用户输入的电子邮件地址字符串格式正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11094658/

相关文章:

iphone - 无法从 UIViewController 添加 UIView

ios - OSStatus 错误 -67028 构建 Ad Hoc iOS 应用程序时

iphone - 在后台检测到其他 iPhone 应用程序已打开

objective-c - NSUserNotification - 小牛和自定义图像

ios - 选择一个单元格显示一个按钮,取消选择隐藏该按钮

ios - 后台执行响应推送通知 - ios

java - 当文本是否包含在双引号中时转义文本中的特殊字符

javascript - CSS 通配符选择

ios - MKMapView 上的引脚注释上的触摸事件

python - 基于文本的键值列表的优雅解析