mvc [DataType(DataType.EmailAddress) no validation
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!
At the moment I have solved my problem using DataAnnotationsExtensions
it just works, you add their library with NuGet
using DataAnnotationsExtensions;
[Required]
[DataType(DataType.EmailAddress)]
[Email]
public string Email { get; set; }
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" />
.