ASP.NET-MVC 2 DataAnnotations StringLength

griegs picture griegs · Mar 22, 2010 · Viewed 25.1k times · Source

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 sharing the source?

Answer

Jim Geurts picture Jim Geurts · Mar 22, 2010

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; }