views:

75

answers:

2

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

+1  A: 

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.

Aliostad
Great thanks 1 more Question for this phonenumber validator how can I allow i.e "(" or ")" or "+" or "-"
StevieB
atm for phonenumbers I have ^[+\d ]+$
StevieB
Stevie, can you ask the question with a bit more info? Similar to this question which you gave good examples.
Aliostad
Yeah I would like to allow a phone number such as +00(49) 87956325412 at the moment my validation only allows numbers ^[+\d ]+$
StevieB
Try this: ^\+?\(001\)|(00\(\d\d)\)\s*\d{8,12}$
Aliostad
No didnt seem to like the "("
StevieB
Make sure you have no space after the telephone. I am using Expresso and your example works,.
Aliostad
I tried the following
StevieB
<code><asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ValidationExpression="^\+?(001)|(00(\d\d))\s*\d{8,12}$" ControlToValidate="txtPhone" Display="None" EnableClientScript="false" ErrorMessage="Please provide valid phone number."> </asp:RegularExpressionValidator></code> and phone number +00(49) 87956325412. And it fails the validator i.e it thinks it is an invalid number
StevieB
Use Expresso http://www.ultrapico.com/Expresso.htm and see if it works in there. If so, make a new question and put the link here so that I can check it up.
Aliostad
+1  A: 

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.

Lazarus
+1. Didn't know we have an RFC for it.
Aliostad