ASP.NET MVC data annotations client side validation with inherited RegularExpressionAttribute

前端 未结 2 1904
生来不讨喜
生来不讨喜 2020-12-08 11:09

To keep my model validation clean I would like to implement my own validation attributes, like PhoneNumberAttribute and EmailAttribute. Some of the

2条回答
  •  时光取名叫无心
    2020-12-08 11:44

    extending of the right answer

    public class EmailAttribute : RegularExpressionAttribute
    {
        static EmailAttribute()
        {
            DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(EmailAttribute), typeof(RegularExpressionAttributeAdapter));
        }
    
        public EmailAttribute()
            : base(@"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$") //^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$
        {
    
        }
    }
    

提交回复
热议问题