StevieB
StevieB

Reputation: 6543

ASP.NET Allow + Symbol in email validation expression

Hey I have the follow email validation expression

^[\w-.]+@([\w-]+.)+[\w-]{2,4}$

What I am looking for is for email addresses to be allowed which have a "+" before the @ part of the email cheers

Upvotes: 1

Views: 2255

Answers (2)

Lazarus
Lazarus

Reputation: 43094

Or use a full RFC 822 regex

^((?>[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+\x20*|"((?=[\x01-\x7f])[^"\\]|\\[\x01-\x7f])*"\x20*)*(?<angle><))?((?!\.)(?>\.?[a-zA-Z\d!#$%&'*+\-/=?^_`{|}~]+)+|"((?=[\x01-\x7f])[^"\\]|\\[\x01-\x7f])*")@(((?!-)[a-zA-Z\d\-]+(?<!-)\.)+[a-zA-Z]{2,}|\[(((?(?<!\[)\.)(25[0-5]|2[0-4]\d|[01]?\d?\d)){4}|[a-zA-Z\d\-]*[a-zA-Z\d]:((?=[\x01-\x7f])[^\\\[\]]|\\[\x01-\x7f])+)\])(?(angle)>)$

And the RegexLib is great place for finding regex for most requirements, you are rarely the first to want to do something.

Upvotes: 1

Aliostad
Aliostad

Reputation: 81680

Just change it to ^[\w-.]+(?:\+[\w]*)?@([\w-]+.)+[\w-]{2,4}$

This allows for + before @ and some characters which is optional. So [email protected] [email protected] are both valid.

Updated.

Upvotes: 1

Related Questions