ios - Regex Hanging in iPhone For Email Validation -
i using following regular expression 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 iphone hanging when enter invalid email has 20-25+ characters before @
symbol.
for example, email address "abcdefghijklmnopqrstuvwxyz@gmail"
cause iphone hang. "abcdefghijklmnopqrstuvwxyz@gmail.com"
validate normally. "abcdefghijklmnopqrst@gmail"
return invalid immediately.
i notice around 20 characters take longer regex return invalid, incrementing 1 character take seemingly exponentially longer.
it seems has part of expression:
([-.[a-za-z0-9_]]*[0-9a-za-z_])*
but can't come alternative gives same result.
any ideas?
there a project on github has tested regex email - very hard craft 1 - see this link on testing various patterns. project has method isvalidemail
can call iteratively (as user taps in info) can enable submit button etc.
you can read more problem , other solutions in this previous answer.
edit: seems ambiguity in regex can cause cycles take infinite time resolve. since posting answer i've been working on "near-perfect" regex validate email, based on standard. github project has been updated, , validation regex is:
@"^(?:(?:(?:(?: )(?:(?:(?:\t| )\r\n)?(?:\t| )+))+(?: ))|(?: )+)?(?:(?:(?:[-a-za-z0-9!#$%&'+/=?^
{|}~]+(?:\\.[-a-za-z0-9!#$%&'*+/=?^_
{|}~]+)*)|(?:\"(?:(?:(?:(?: )*(?:(?:[!#-z^-~]|\[|\])|(?:\\(?:\t|[ -~]))))+(?: )*)|(?: )+)\"))(?:@)(?:(?:(?:a-za-z0-9?)(?:\.a-za-z0-9?)*)|(?:\[(?:(?:(?:(?:(?:[0-9]|(?:[1-9][0-9])|(?:1[0-9][0-9])|(?:2[0-4][0-9])|(?:25[0-5]))\.){3}(?:[0-9]|(?:[1-9][0-9])|(?:1[0-9][0-9])|(?:2[0-4][0-9])|(?:25[0-5]))))|(?:(?:(?: )*[!-z^-~])*(?: )*)|(?:[vv][0-9a-fa-f]+\.[-a-za-z0-9.~!$&'()+,;=:]+))\])))(?:(?:(?:(?: )(?:(?:(?:\t| )\r\n)?(?:\t| )+))+(?: ))|(?: )+)?$"
Comments
Post a Comment