tags:

views:

83

answers:

3

I am using the following regular expression to validate e-mails, but it allows empty strings as well, how can I change it to prevent it:

^[\w\.\-]+@[a-zA-Z0-9\-]+(\.[a-zA-Z0-9\-]{1,})*(\.[a-zA-Z]{2,3}){1,2}$

I am using an asp:RegularExpressionValidator. My other option is to add on a asp:RequiredFieldValidator, but I am curious if this is possible to check for blanks in my RegularExpressionValidator, so I don't have to have 2

A: 

This RegEx validates if a given string is in a valid email-format or not:

/^[a-zA-Z0-9\_\-\.]+\@([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,4}$/
Erik
When I put this into a asp:RegularExpressionValidator, it tells me it is not a valid Regular Expression.
Xaisoft
Not really, some allowed characters (e.g. +) are missing
Ofir
I'm new to regular expressions, so I have no idea how to read it. I just found the one above from the web.
Xaisoft
@Xaisoft: then it would be a good idea to add the information, that you need the RegEx for ASP. This is a Perl-RegEx. @Ofir: IMHO + isn't allowed in an email-adsress and I have never seen one up to now. But if so, we can add it as allowed character too :)
Erik
@Xaisoft: have you removed the "/" at the beginning and in the end of the RegEx?
Erik
The `+` character *is* valid!
Peter Boughton
@Erik: I removed the "/" at the beginning and end, but it was still invalid for asp.net. Did you edit the regular expression from your original post?
Xaisoft
And you don't need to escape `_` or `.` in char class, or `@` at all, and this wont accept `[email protected]` or `[email protected]` etc
Peter Boughton
@Peter Boughton: Indeed - i found it in RFC2822. My fault. But it is unusual ;) @Xaisoft - no I did not edit. I would recommend you to use the RegEx from the site of Ofir.
Erik
It's not unusual. It's common enough that gmail explicitly supports it.
Peter Boughton
@Peter: well - unusual enough, so that I haven't seen in in the past 17 years :) But your right. It is defined and it is valid.
Erik
+3  A: 

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

Ofir
+1 did not know this site - very good link!
Erik
I have seen this site many times. It is very good. Thanks for the link again. I found some good stuff on here.
Xaisoft
+1  A: 

That expression does not match empty strings. The expression starts with ^[\w\.\-]+ this translates to "The string must start with a word character, period or slash. There can be more than one of these." There must be something else wrong or you copied the expression incorrectly.

unholysampler
Or the ASP bit is accepting empty strings despite the regex preventing them - I think I've seen something like that mentioned before.
Peter Boughton
There is your answer. "Validation succeeds if the input control is empty. If a value is required for the associated input control, use a RequiredFieldValidator control in addition to the RegularExpressionValidator control." http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.regularexpressionvalidator.aspx
unholysampler