我正在检查我的应用程序中的电子邮件验证,但这没有发生,我正在使用 Xcode 8

I am checking email validation in my app but that is not happening and I am using Xcode 8

我正在检查电子邮件的验证但未完成以下是我的代码。 请告诉我背后的问题。

-(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 [emailTest evaluateWithObject:candidate];
}




-(BOOL)RagistationValidation{

    if ([_txtmail.text isEqualToString:@""] ) {

        UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"Wrong Email Id" message:@"Please enter Emailid" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *ok=[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:ok];
        [self presentViewController:alert animated:YES completion:nil];

    }else if ([self validateEmail:_txtmail.text]){

        UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"Wrong Email Id" message:@"Please enter valid Emailid" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *ok=[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:ok];
        [self presentViewController:alert animated:YES completion:nil];


    }

请帮帮我。

你的正则表达式应该是这样的,所以请替换它以进行电子邮件验证

 +(BOOL)isValidEmailID:(NSString *)email
    {
        NSString *regExPattern = @"[_A-Za-z0-9-]+(\.[_A-Za-z0-9-]+)*@[A-Za-z0-9][A-Za-z0-9]+(\.[A-Za-z0-9][A-Za-z0-9]+)*(\.[a-zA-Z]{2,4})+";

        NSPredicate * emailValidator = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regExPattern];

        BOOL isValid = [emailValidator evaluateWithObject:email];
        return isValid;
    }

检查

if(![self isValidEmailID:self.txt_username.text]){