mvc [DataType(DataType.EmailAddress) no validation

2019-01-23 12:11发布

问题:

I'm using this code on an email field:

    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = "Email address")]
    public string Email { get; set; }

[DataType(DataType.EmailAddress)] does not work (validation does not occur no at a server not on the client side).

I am not sure if I should implement myself a Custom Attribute or I can use one included with MVC 3.

Could you please suggest me a solution for creating a custom attribute in case I need to.

I read also about some additional extensions, example http://nuget.org/packages/DataAnnotationsExtensions.MVC3

Would you suggest it to me?

回答1:

You could use the usual DataAnnotations library by just using [EmailAddress]

using System.ComponentModel.DataAnnotations;
    [Required]
    [EmailAddress]
    public String Email { get; set; }

Also just for reference, here's the regular expression version of this validation:

    [RegularExpression(@"^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-‌​]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$", ErrorMessage = "Email is not valid")]
    public String Email {get; set;}

Best of luck!



回答2:

At the moment I have solved my problem using http://dataannotationsextensions.org

it just works, you add their library with NuGet

using DataAnnotationsExtensions;


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


回答3:

It looks like all the answers focus on the Data Model while this issue can be affected by the View itself.

The following on MVC .NET 4.5 is working alright:

Data model:

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

Razor View:

@Html.LabelFor(model => model.Email)
@Html.EditorFor(model => model.Email)

Note: do not need to add [EmailAddress] attribute. If you use [DataType(DataType.EmailAddress)] along with @Html.EditorFor() in your View, you should be fine.

As highlighted with rich.okelly, at the end you want your input rendered as <input type="email" />.



回答4:

May be this will be helpful for someone. Following works for me

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

But does not work following

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

I am using MVC 5 & .NET 4.5



回答5:

As Felix mentioned, the problem is on the View level, you need to use EditorFor() in your View instead of TextBoxFor(), the EditorFor() will render:

<input type="email" />

which will trigger the validation, while TextBoxFor() will render:

<input type="text" />

So in order to validate your entered email address, you need (in combination with EditorFor()) to use only:

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

This way, your entered value for email will be always validated, but if you don't enter a value for email, nothing will happen (unless you specified the [Required] attribute), the form will be submitted with an empty email address.