Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular expression not working

Status
Not open for further replies.

tshad

Programmer
Jul 15, 2004
386
0
0
US
I have a regular expression that works fine for emails but it also allows angle brackets "<" and ">".

It crashes the page if there are angle brackets in the email.

My expression is: @"[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}"

I would assume this would not work since I say only allow certain letters and none are angle brackets.

It allows "tom <tom@aol.com>" to go through fine.

Why does it work and what can I do to fix it?

Thanks,

Tom
 
The cuplrit is the period in your second set: [A-Z0-9.-]+
. is a special character in Regex meaning "any character". You are looking for a literal period, hence escape it:
[A-Z0-9\.-]+

Does that do the trick?

Cheers,
MakeItSo

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Didn't work.

I have it as:

private const string Regex_Email = @"[A-Z0-9\._%+-]+@[A-Z0-9\.-]+\.[A-Z]{2,4}";

and the code is:

reg = new Regex(Regex_Email, RegexOptions.IgnoreCase);
result = (reg.IsMatch(email));

and the result comes back as true.
 
I think I know the problem:
<tom@aol.com> goes through because tom@aol.com is a match.

Maybe you should rethink your approach and first check for < > addresses, maybe replacing their "@" with [at]" before testing for mail addresses?

“Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family.” (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top