ASP.NET MVC 2 - Multiple Regex on a property

后端 未结 3 751
小鲜肉
小鲜肉 2021-01-07 04:36

I would like to know if a way exists in asp.net mvc 2, to have multiple regular expression on a proprety. For example :

[RegularExpression(\"[^0-9]\", Error         


        
3条回答
  •  礼貌的吻别
    2021-01-07 05:20

    Change attribute usage with a custom class

    The best solution is of course to create a custom attribute that inherits from RegularExpressionAttribute but sets different attribute usage settings. The main setting is AllowMultiple that you need to set to true.

    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple=true)]
    public class MultiRegularExpressionAttribute: RegularExpressionAttribute
    {
        ...
    }
    

    You would use this attribute just like you use the existing RegularExpressionAttribute, but you'd have the ability to put multiple of them on the same property.

提交回复
热议问题