Email model validation with DataAnnotations and DataType

前端 未结 4 1730
说谎
说谎 2020-12-01 06:08

I have following model:

public class FormularModel
{
    [Required]
    public string Position { get; set; }
    [Required]
    [DataType(DataType.EmailAddre         


        
4条回答
  •  一向
    一向 (楼主)
    2020-12-01 06:39

    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.

提交回复
热议问题