regex - how does one validate the "." in e-mail addresses? -
i have following method validate e-mail addresses in form.
-(bool) validemailaddress:(nsstring*) emailstring { nsstring *regexpattern = @"^[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]{2,4}$"; nsregularexpression *regex = [[nsregularexpression alloc] initwithpattern:regexpattern options:nsregularexpressioncaseinsensitive error:nil]; nsuinteger regexmatches = [regex numberofmatchesinstring:emailstring options:0 range:nsmakerange(0, [emailstring length])]; if (regexmatches == 0) return no; // email invalid else return yes; // email valid } this works great except breaks down if user gives e-mail of "abc@xyz" (without super domain .com or .org).
i'm pretty sure problem has "." in regex. tried escaping this,
nsstring *regexpattern = @"^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$"; but doesn't work, either. suggestions?
edit: didn't put in escape before last dot when posting question. sorry.
i don't see escape sequence in last example. try:
nsstring *regexpattern = @"^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4}$";
Comments
Post a Comment