So is an [Email] attribute built in ASP.NET MVC 3 or not?

前端 未结 4 1691
小鲜肉
小鲜肉 2021-02-20 03:11

The [Email] attribute was going to be built into ASP.NET MVC 3 as it was in futures? So is it now available or not? I guess it is quite a dumb question but I\'ve sp

4条回答
  •  再見小時候
    2021-02-20 03:56

    If [Email] is supposed to be a data annotation for MVC models (like [Required]), then it's not built into ASP.NET MVC 3.

    All the model data annotations are found in the namesace System.ComponentModel.DataAnnotations. There you find classes like RequiredAttribute.

    Update:

    It's pretty easy to add the Email attribute:

    using System.ComponentModel.DataAnnotations;
    
    namespace YourNamespace
    {
        public class EmailAttribute : RegularExpressionAttribute
        {
            public EmailAttribute() : base("[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?")
            {
            }
        }
    }
    

    What the best or correct regular expression for checking email addresses is, is a separate and long debate on the net. I'm not insisting that mine is either of it.

提交回复
热议问题