views:

592

answers:

2

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?

+1  A: 

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; }
Jim Geurts
No we're not using 4.0 just yet and the way things happen here it won't be for a long time yet. :)
griegs
A: 

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

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