25

I have an MVC 4 web application and I need to enter and validate some email addresses, without sending an email to the user's email address.

Currently I am using basic regex email validation with this pattern:

[RegularExpression(@"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z",
        ErrorMessage = "Please enter correct email address")]

Although this is validating email addresses, it passes 1@1.1 as a valid email address. For the moment I have a validation that requires symbols @ symbols . symbols where the symbols can be numeric/alphabetic and ._- .

I need more standard email validation for my MVC 4 application. How do I do that?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
dlght
  • 1,406
  • 1
  • 18
  • 35

6 Answers6

83

You need a regular expression for this. Look here. If you are using .net Framework4.5 then you can also use this. As it is built in .net Framework 4.5. Example

[EmailAddress(ErrorMessage = "Invalid Email Address")]
public string Email { get; set; }
Ehsan
  • 31,833
  • 6
  • 56
  • 65
24

Expanding on Ehsan's Answer....

If you are using .Net framework 4.5 then you can have a simple method to verify email address using EmailAddressAttribute Class in code.

private static bool IsValidEmailAddress(string emailAddress)
{
    return new System.ComponentModel.DataAnnotations
                        .EmailAddressAttribute()
                        .IsValid(emailAddress);
}

If you are considering REGEX to verify email address then read:

I Knew How To Validate An Email Address Until I Read The RFC By Phil Haack

Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
  • Strangely, a trailing period at the end of the email passes validation. – TTT Mar 09 '16 at 20:36
  • @TTT, did you find a way to validate trailing period case? – Ammar Khan Dec 27 '16 at 16:25
  • @user2866746 - it's because a trailing period is valid for the domain name, so I just left it. It should resolve just fine. – TTT Dec 27 '16 at 16:37
  • But in my case, I'm seeing the email not getting sent to the email address which contain a trailing period. – Ammar Khan Dec 27 '16 at 16:47
  • One of the end user entered (Eg: `abc@mailinator.com.`) which is correctly validate by the Model binder but the problem is, email can not be send to this address and it says, `A valid address is required.` – Ammar Khan Dec 27 '16 at 16:55
16

Regex:

[RegularExpression(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", ErrorMessage = "Please enter a valid e-mail adress")]

Or you can use just:

    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }
whoah
  • 4,363
  • 10
  • 51
  • 81
  • 2
    I would up using a combination of both. Apparently a@a is a valid email, or at least it passes the EmailAddress attribute validation. – CBC_NS Dec 09 '16 at 20:37
4

Why not just use the EmailAttribute?

[Email(ErrorMessage = "Bad email")]
public string Email { get; set; }
dav_i
  • 27,509
  • 17
  • 104
  • 136
1

Don't.

Use a regex for a quick sanity check, something like .@.., but almost all langauges / frameworks have better methods for checking an e-mail address. Use that.

It is possible to validate an e-mail address with a regex, but it is a long regex. Very long.
And in the end you will be none the wiser. You'll only know that the format is valid, but you still don't know if it's an active e-mail address. The only way to find out, is by sending a confirmation e-mail.

SQB
  • 3,926
  • 2
  • 28
  • 49
  • The confirmation e-mail is the thing i cannot do. I need just to add some emails in a list for later function with a service reporting tool. Problem solved though so thx for the time ;] – dlght Dec 05 '13 at 13:19
0

It is surprising the question of validating an email address continually comes up on SO!

You can find one often-mentioned practical solution here: How to Find or Validate an Email Address.

Excerpt:

The virtue of my regular expression above is that it matches 99% of the email addresses in use today. All the email address it matches can be handled by 99% of all email software out there. If you're looking for a quick solution, you only need to read the next paragraph. If you want to know all the trade-offs and get plenty of alternatives to choose from, read on.

See this answer on SO for a discussion of the merits of the article at the above link. In particular, the comment dated 2012-04-17 reads:

To all the complainers: after 3 hours experimenting all the solutions offered in this gigantic discussion, this is THE ONLY good java regex solution I can find. None of the rfc5322 stuff works on java regex.

Community
  • 1
  • 1
DavidRR
  • 18,291
  • 25
  • 109
  • 191
  • Should i again answer that i don't need basic validation and I was looking for a way to validate email in MVC4 application.As pointed out there is other way: with attribute that i forgot to put on.I have used the given example and yet the email validation with that regex expression was passing bad email formats for my case. – dlght Dec 05 '13 at 13:48