DataAnnotations StringLength Attribute MVC – without max value

Is there a way to only specify only a minimum length? Perhaps another
attribute i can use?

Using the standard data annotation, No. You must specify the MaximumLength. Only the other parameters are optional.

In such a case, I’d recommend something like this:

[StringLength(int.MaxValue, MinimumLength = 7)]

You can also use a Regex (regular expression) attribute like this one:

[RegularExpression(@"^(?:.*[a-z]){7,}$", ErrorMessage = "String length must be greater than or equal 7 characters.")]

More on this here: Password Strength Validation with Regular Expressions

Leave a Comment