Uses of Datatype.EmailAddress in asp/.net/mvc

前端 未结 4 1836
遇见更好的自我
遇见更好的自我 2020-12-16 22:39

I have a Account Model in which I am using Email Address as Username

public class RegisterModel
    {
        [Required]
        [Display(Name = \"Email Add         


        
4条回答
  •  萌比男神i
    2020-12-16 23:30

    Datatype.Emailaddress derives from DataTypeAttribute and adds client-side e-mail validation you also need to set <% Html.EnableClientValidation(); %> in your corresponding view.

    Alternatively you could use the DataAnnotations library by using EmailAddress (This performs server side validation)

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

    This is the regex to validate Email address

    [Required(ErrorMessage="Email is required")]
    [RegularExpression(@"[A-Za-z0-9._%+-]+[A-Za-z0-9.-]+\.[A-Za-z] {2,4}",
    public String Email {get; set;}
    

    You can also create custom email validation Attribute.

    http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

提交回复
热议问题