Avatar of Camillia
Camillia
Flag for United States of America asked on

Regular expression: validate email

I found this regualr expression to validate email but it accepts emails like this:

 
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"/>

Open in new window

123@4.3

Microsof's site has that : http://msdn.microsoft.com/en-us/library/aa479013.aspx
but 123@4.3 is not a valid email addess....


I need valid email addresses like test_testing@test.com or .gov, etc


Tried these 2 as well but they didnt accept examples like this :something@test.com.

"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"

--and this one

b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b

Open in new window

.NET Programming

Avatar of undefined
Last Comment
Terry Woods

8/22/2022 - Mon
WebF00L

Hi,


Give
\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b

Open in new window

A try

http://www.regular-expressions.info/regexbuddy/email.html

//WebFooL Untangle Evangelist
BuggyCoder

try this:-

^((?>[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)>)$

Open in new window


http://regexlib.com/DisplayPatterns.aspx?AspxAutoDetectCookieSupport=1
http://www.codeproject.com/Articles/22777/Email-Address-Validation-Using-Regular-Expression
Camillia

ASKER
This one, as I posted, doesnt work. For example, I try test@i.com. It gets rejected by this

\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b

Open in new window


The one posted by BuggyCoder doesnt work. I plug it in ASP.Net aspx page and there are characters that throw errors.

Should I be using the one Microsoft is using?
Your help has saved me hundreds of hours of internet surfing.
fblack61
BuggyCoder

well well well, if you are using it in aspx page then do something like this:-

 public const string MatchEmailPattern =
                  @"^(([\w-]+\.)+[\w-]+|([a-zA-Z]{1}|[\w-]{2,}))@"
     + @"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
                        [0-9]{1,2}|25[0-5]|2[0-4][0-9])\."
     + @"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\.([0-1]?
                        [0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
     + @"([a-zA-Z]+[\w-]+\.)+[a-zA-Z]{2,4})$";

just add @ before string, i hope you are using c#.
Try visiting this link:-
http://www.codeproject.com/Articles/22777/Email-Address-Validation-Using-Regular-Expression
Camillia

ASKER
i am using C# but what you have throws errors. This is what i have now:

<asp:RegularExpressionValidator  ID="RegularExpressionValidator1" runat="server"  
                                      
                                     Text="Invalid email address!"
                                    ErrorMessage="Invalid email address!" 
                                    ControlToValidate="txtEmail"     
                                    ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"/>

Open in new window


Yes, i saw that link as well. But there has a be a better way of doing this. How's the performance of using such a long regualr expression??
BuggyCoder

this is the one i found on MSDN:-

^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$

try to add escape characters when you wrap it in a string variable on in control parameter........ or simply replace  " with '(single quote)
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Camillia

ASKER
I changed double quotes to single but get syntax error. Not sure what you mean by escape characters...


 ValidationExpression="^(?('')(''.+?''@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$"/>

Open in new window

Terry Woods

You might find this article interesting: http://www.regular-expressions.info/email.html

It's the same site as WebF00L's link, but with explanations.
BuggyCoder

here have a look at what are escape sequences or characters:-
http://msdn.microsoft.com/en-us/library/ms228362.aspx
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Camillia

ASKER
you think i should use that one? test@i.com is not valid probably because of "i.com"....that probably below doesnt allow:

\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b

Open in new window


Or should I stick with what MS has but it allows stuff like 123@4.3

ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"/>

Open in new window

Terry Woods

Maybe you should ask yourself what are the consequences of an invalid email address? If you have users setting up an account of some sort, it's a common technique to send an email to them to validate it's really them. Otherwise even a valid email address may not have the desired person at the other end.
Camillia

ASKER
Terry, you think i should stick with what MS has in their site?
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"/>

We won't use the email to send validation emails. It's just to have the user's email address. I think it would be ok to keep the same regualr expression that MS has, correct?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Terry Woods

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Camillia

ASKER
let me try that.
Camillia

ASKER
The latest one accepts 123@4.5  and I'm assuming it's ok because I want it to be flexible. Correct?
Terry Woods

Yes.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck