I use the following regular expression to validate email:
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 am facing is that the iPhone hangs when I enter an invalid email with 20-25 + characters before the @ character.
For example, the email address " Abcdefghijklmnopqrstuvwxyz@gmail " will cause the iPhone to freeze. but " Abcdefghijklmnopqrstuvwxyz@gmail.com " will be checked normally. " Abcdefghijklmnopqrst@gmail " will return immediately.
I notice about 20 characters that it will take longer for the regular expression to return the value invalid, and then an increase of 1 character will look exponentially longer.
This seems to have 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?
source share