0

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

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Xaisoft
  • 45,655
  • 87
  • 279
  • 432
  • 1
    possible duplicate of [What is the best regular expression for validating email addresses?](http://stackoverflow.com/questions/201323/what-is-the-best-regular-expression-for-validating-email-addresses) – pkaeding Jul 20 '10 at 14:50

3 Answers3

3

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

Ofir
  • 8,194
  • 2
  • 29
  • 44
  • I have seen this site many times. It is very good. Thanks for the link again. I found some good stuff on here. – Xaisoft Jul 20 '10 at 14:55
1

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
  • 17,141
  • 7
  • 47
  • 64
  • 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 Jul 20 '10 at 15:16
  • 2
    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 Jul 20 '10 at 15:21
-1

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
  • 3,777
  • 21
  • 27
  • When I put this into a asp:RegularExpressionValidator, it tells me it is not a valid Regular Expression. – Xaisoft Jul 20 '10 at 14:46
  • 1
    Not really, some allowed characters (e.g. +) are missing – Ofir Jul 20 '10 at 14:47
  • 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 Jul 20 '10 at 14:49
  • 1
    @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 Jul 20 '10 at 14:50
  • @Xaisoft: have you removed the "/" at the beginning and in the end of the RegEx? – Erik Jul 20 '10 at 14:51
  • 1
    The `+` character *is* valid! – Peter Boughton Jul 20 '10 at 14:54
  • @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 Jul 20 '10 at 14:56
  • And you don't need to escape `_` or `.` in char class, or `@` at all, and this wont accept `user@history.museum` or `contact@airport.travel` etc – Peter Boughton Jul 20 '10 at 14:57
  • @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 Jul 20 '10 at 14:57
  • It's not unusual. It's common enough that gmail explicitly supports it. – Peter Boughton Jul 20 '10 at 14:58
  • @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 Jul 20 '10 at 15:01