2

I am building an MVC 4 web application and using Fluent Validation when validating some of my ViewModels.

I need to validate Email addresses and I was using this regular expression, it's the default one that comes in the AccountModels class when you create a default MVC application in Visual Studio

RuleFor(x => x.ConfirmEmail)
.Matches(@"^[\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$")
.WithMessage("Please enter a valid email address");

However, my client has told me this is not accepting email addresses which include apostrophes before the @ sign.

I am not good with regular expressions at all, so could someone please help me to amend this reg expressive to allow for apostrophes before the @ sign?

Thanks in advance.

Update

I had put in the wrong code sample, please see the regular expression now - this is the one that does not accept apostrophes.

tereško
  • 58,060
  • 25
  • 98
  • 150
tcode
  • 5,055
  • 19
  • 65
  • 124
  • Looks like it should match even with apostrophe. Can you provide the non matching email (perhaps change the domain name in it, and a few letters, for privacy). I suspect there might be another problem. – Billy Moon Apr 17 '13 at 15:11
  • I agree, it clearly accepts them: `^([\w-\.']+)@` you can see the apostrophe in the character class... Oh and btw, you don't need to escape the dot inside the character class. – Loamhoof Apr 17 '13 at 15:14
  • 3
    Pretty please, don't use regex to validate email addresses. See also http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address and a large number of related questions. – tripleee Apr 17 '13 at 15:47
  • Also, this regex is wacky. It will fail on top-level domains longer than six characters (there are several now) and what's with the literal IP address with exactly a four-digit port number? Even without the port number nonsense, it's wrong. – tripleee Apr 17 '13 at 15:50
  • 2
    **Don't roll your own solution to this problem.** If ever there is a programming problem where it makes sense to rely on existing code that has been written, tested and debugged, this is it. – Andy Lester Apr 17 '13 at 15:53
  • For the record, if you have to have an email address with a literal IP address, the correct format is with brackets; `you@[10.9.87.6]`. There is no support for port numbers, with IP addresses or otherwise. (I see now that I misread the grouping for the port number; the regex will accept `you@example.com:1234` also.) – tripleee Apr 17 '13 at 16:03

1 Answers1

6

After your update, the regular expression you are looking for is:

^[\w-']+(\.[\w-']+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$

Note, the only thing changed here, is that the ' (apostrophe) character has been added in each of the two character classes before the @ symbol.

femtoRgon
  • 32,893
  • 7
  • 60
  • 87
  • This doesn't seem to work when the user enters a completely uppercase email address. Though it's not a great use case to do this, it is something that people do - and the regex should support it. – csharpforevermore Aug 07 '13 at 15:03
  • @CSharpForEverMore - There are **many** forms of [valid e-mail address](http://en.wikipedia.org/wiki/Email_address#Valid_email_addresses) that are not supported by this, such as: `"pretty \"strange\" address"@[IPv6:2001:db8:1ff::a0b:dbd0]`. My goal was just to add enough to handle the very specific issue stated in the question. – femtoRgon Aug 07 '13 at 15:13