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

57 views Asked by At

I am checking validation of email but not done following are my code. Please tell me what the issue behind it.

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


    }

Please help me.

1

There are 1 answers

1
Jitendra Modi On BEST ANSWER

Your Regex Should be like this, So please replace it for email validation

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

FOR CHECKING

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