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

时间:2016-12-24 04:59:45

标签: objective-c xcode8 email-validation

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

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


    }

请帮帮我。

1 个答案:

答案 0 :(得分:0)

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

 +(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]){