mvc [DataType(DataType.EmailAddress) no validation

后端 未结 5 866
情歌与酒
情歌与酒 2020-12-15 16:56

I\'m using this code on an email field:

    [Required]
    [DataType(DataType.EmailAddress)]
    [Display(Name = \"Email address\")]
    public string Email          


        
相关标签:
5条回答
  • 2020-12-15 17:20

    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

    0 讨论(0)
  • 2020-12-15 17:31

    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.

    0 讨论(0)
  • 2020-12-15 17:33

    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; }
    
    0 讨论(0)
  • 2020-12-15 17:35

    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" />.

    0 讨论(0)
  • 2020-12-15 17:39

    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!

    0 讨论(0)
提交回复
热议问题