ASP.NET-MVC 2 DataAnnotations StringLength

前端 未结 2 709
感情败类
感情败类 2021-02-02 08:47

Can I use the MVC 2 DataAnnotations to specify a minimum length for a string field?

Has anyone done this or have they created custom attributes and if so do you mind sha

2条回答
  •  南旧
    南旧 (楼主)
    2021-02-02 08:55

    Use a regular expression attribute. These are interpreted on the client side as well.

    [RegularExpression(Regexes.MinStringLength)]
    public string MyText { get; set; }
    

    Where Regexes.MinStringLength is a static regular expression class. Inline would look like this:

    [RegularExpression(@"^.{5,10}$")] // valid five to ten characters
    public string MyText { get; set; }
    

提交回复
热议问题