ASP.NET-MVC 2 DataAnnotations StringLength

▼魔方 西西 提交于 2019-12-02 20:15:02

If you're using asp.net 4.0, you can use the StringLength attribute to specify a minimum length.

Eg:

[StringLength(50, MinimumLength=1)]
public string MyText { get; set; }

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; }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!