I am using the following regular expression for email validation:
NSString *emailRegEx = @"([0-9a-zA-Z]([-.[A-Za-z0-9_]]*[0-9a-zA-Z_])*@([0-9a-zA-Z][-[A-Za-z0-9_]]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})";
NSPredicate *emailRegexPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", firstPartRegex];
return [emailRegexPredicate evaluateWithObject:input]
The problem I'm having is that iPhone is hanging when I enter an invalid email that has about 20-25+ characters before the @
symbol.
For example, the email address "Abcdefghijklmnopqrstuvwxyz@gmail"
will cause iPhone to hang.
but "Abcdefghijklmnopqrstuvwxyz@gmail.com"
will validate normally. "Abcdefghijklmnopqrst@gmail"
will return invalid immediately.
I notice around 20 characters that it will take longer for regex to return invalid, then incrementing by 1 character will take seemingly exponentially longer.
It seems that it has something to do with this part of the expression:
([-.[A-Za-z0-9_]]*[0-9a-zA-Z_])*
but I can't come up with an alternative that gives the same result.
Any ideas?