tags:

views:

100

answers:

5

Hi All,

I am using ^[\w-\.\+]+@([\w-]+\.)+[\w-]{2,4}$ to validate email address, when I use it from .aspx.cs it works fine to validate IDN email but when I use it from aspx page directly it doesn't work.

return  Regex.IsMatch(
                email,
                @"^[\w-\.\+]+@([\w-]+\.)+[\w-]{2,4}$",
                RegexOptions.Singleline);

the ID that I would like to validate looks like pelai@ÖßÜÄÖ.com

I am too bad at regex do you guys know what am I doing wrong?

+2  A: 

First the correct validation of an e-mail address is somewhat more complex as regex. But that apart, the Regex is not at fault, but probably rather how you use it.

Edit (after seeing your code): do you make sure that the string to be tested has no whitespace and such in it? Put a breakpoint on it right there and inspect the string, that might give you an idea of what is going wrong.

Lucero
@Lucero, it works fine from .aspx.cs and I do check the string on server side but I want to save a server trip and therefore want to validate clientside and in that process when I add the above regex to asp:RegularExpressionValidator, it dosent work.
Pranali Desai
I see. Try this: `^[\w\-\.\+]+@([\w\-]+\.)+[\w\-]{2,4}$`
Lucero
@Lucero, :( dosent work I tested for pelai@ÖßÜÄÖ.com.
Pranali Desai
Should that match or not? The ECMA regex matches the `\w` differenty, which does *not* include the ÜÄÖ (and like) characters. You can actually enable the same behavior on the .NET side by using the `EcmaScript` option. For more info on the JS regex, see http://www.regular-expressions.info/javascript.html
Lucero
+2  A: 

You should escape dash (-) within the first char class and no need for dot and plus :

[\w\-.+]

or

[\w.+-]

no need to escape dash if it is the last char.

M42
.+ would match 1 or more of any character, but he is trying to match the literal '.' and '+'. He might as well escape everything in there.
plor
@plor: Inside a character class, `.` means `.` and `+` means `+`. The only characters that would need to be escaped are `-`, `]`, and the backslash itself.
Alan Moore
+3  A: 

You may want to take a look at regexlib.com, they have a fantastic selection of user-created content to do these extremely commont types of matches.

http://regexlib.com/Search.aspx?k=email

andyortlieb
+1 for not re-inventing the wheel
bta
A: 

With "directly from aspx page" you probably mean in a regularexpression validator?

Then you need to be aware that the regex is used by a different system: javascript which has it's own implementation of regex. This means that regexes that work in .Net directly, might fail in js.

The implementations are not too different, the basics are identical. But there might be differences in details (as how an unescaped - is handled) and js lacks some "advanced features" (although your regex doesn't look too "advanced" ;-) ).

Do you see any error messages in the browser?

Hans Kesting
No there is no error in the browser other than "email address is not valid" which I have included in the Text property of the control :),
Pranali Desai
A: 

The problem is those non-ASCII characters in your test address, ÖßÜÄÖ (which you only ever mentioned in a comment to @HansKesting's answer). In .NET, \w matches all Unicode letters and digits, and even several characters besides _ that are classified as connecting punctuation, but in JavaScript it only matches [A-Za-z0-9_].

JavaScript also lacks support for Unicode properties (like \p{L} for letters) and blocks (\p{IsLatin}), so you would have to list any non-ASCII characters you want to allow by their Unicode escapes (\uXXXX). If you just want to support Latin1 letters, I suppose you could use [\w\u00C0-\u00FF], but IDN is supposed to support more than just Latin1, isn't it?

By the way, JavaScript also doesn't support Singleline mode, and even if it did you wouldn't be able to use it. JS does support Multiline and IgnoreCase modes, but there's no way to set them on both the server and client side. The inline modifiers, (?i) and (?m), don't work in JS, and the RegexOptions argument only works server-side.

Fortunately, you don't really need Singleline mode anyway; it allows the . metacharacter to match linefeeds, but the only dots in your regex are matching literal dots.

Alan Moore