0

I am using the following code to check the validation of my email which is entered in the uitextfield. But it is returning true when I enter [email protected] it is not returning false.

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:email];

Anyone can suggest me ?

Sharme
  • 625
  • 4
  • 10
  • 28

2 Answers2

1

Email validation for ios code.

- (BOOL)validateEmailWithString:(NSString*)email
{
    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:email];
}

Textfield delegate

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];

    if ([self validateEmailWithString:textField.text])
    {
        Nslog(@"Valid");
    }
    else
    {
        Nslog(@"Not Valid");
    }

}
Kirit Modi
  • 23,155
  • 15
  • 89
  • 112
1

Don't exclude the domain m.com.com, as it's a possible subdomain of com.com (wich maybe looks strange, but is a valid - and btw. real - domain).

If you still want to exclude all subdomains (saying: only allow one . after @) you would exclude email addresses like:

So, you maybe don't want to exclude them.

lootsch
  • 1,867
  • 13
  • 21