65

I have following model:

public class FormularModel
{
    [Required]
    public string Position { get; set; }
    [Required]
    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }
    [Required]
    public string Webcode { get; set; }
}

Required validation works fine. But when i try with DataType it doesn't react.

Here is my razor code for the email control:

   @Html.TextBoxFor
          (model => model.Email, 
           new { @style = "width: 175px;", @class = "txtField" }
          ) * 

So, anyone know an answer?

TIA

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
lifeofbenschi
  • 777
  • 1
  • 6
  • 12

5 Answers5

148

DataType attribute is used for formatting purposes, not for validation.

I suggest you use ASP.NET MVC 3 Futures for email validation.

Sample code:

[Required]
[DataType(DataType.EmailAddress)]
[EmailAddress]
public string Email { get; set; }

If you happen to be using .NET Framework 4.5, there's now a built in EmailAddressAttribute that lives in System.ComponentModel.DataAnnotations.EmailAddressAttribute.

Matus Nemcik
  • 240
  • 2
  • 13
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
  • 10
    but it accepts 'something@domain' . i guess i will go with regular expression this time. – Anupam Roy Oct 16 '15 at 16:33
  • 2
    As we cant give custom error message. Am using this following way instead. [EmailValidation(ErrorMessage = "The Email Address already exists")] [RegularExpression( "^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9]+)*\\.([a-z]{2,4})$" , ErrorMessage = "Invalid email format." )] [Required(ErrorMessage = "Please enter your e-mail address."), StringLength(50)] public string Email { get; set; } – Pavan N Feb 04 '16 at 13:22
  • 1
    That Regex will fail for "ryan.o'neal@gmail.com" – jksemple Apr 21 '16 at 09:40
8

The DataAnnotationsExtensions project has an Email attribute that you can use.

jrummell
  • 42,637
  • 17
  • 112
  • 171
  • I have already used this one. Pretty good too! I tried to remember the name but couldn't bring it to my mind. Ended up suggesting MVC 3 Futures. – Leniel Maccaferri Jan 24 '12 at 15:22
3

I have looked at the source code (reverse engineered by Reflector) and DataType variants are actually not even implemented! (This was for DateType.Date)

So it is not going to work.

I would personally use RegexValidation for email.


For clarity, here is the implementation of IsValid in class DataTypeAttribute:

public override bool IsValid(object value)
{
    return true;
}
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • 2
    hehehe... everything is valid! =) – Leniel Maccaferri Jan 24 '12 at 15:24
  • 1
    Regex validation of email addresses is a really bad idea; the RFC allows to many weird permutations to make a regex feasible. Even tagged emails, as supported by GMail, usually get rejected as invalid, despite being perfectly valid. (http://davidcel.is/blog/2012/09/06/stop-validating-email-addresses-with-regex/ points this out well). http://programmers.stackexchange.com/questions/78353/how-far-should-one-take-e-mail-address-validation explores the various options, but the only 100% guarantee is to email the address and check for bounces. – David Keaveny Sep 10 '14 at 01:23
2

I used this regex pattern which will allow some of the newer longer extensions (.mynewsite, etc).

@"^[\w-_]+(\.[\w!#$%'*+\/=?\^`{|}]+)*@((([\-\w]+\.)+[a-zA-Z]{2,20})|(([0-9]{1,3}\.){3}[0-9]{1,3}))$"

Not Valid, among others too:

  • email@com
  • email-o'neil@mysite.com <-- doesn't like the tick, oh well.

Examples that work:

  • email@m.superlongextension
RichieMN
  • 905
  • 1
  • 12
  • 33
-6

I think you need to add at html code one componente Html.ValidationMessageFor. This component shows the validation.

The code may be (using razor):

@Html.TextBoxFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)

try it.

Cezar
  • 55,636
  • 19
  • 86
  • 87