Validating String Length In The Model

The ability to validate values passed into an ASP.NET application at the model level is a powerful thing.

You can validate if the correct type of value has been passed in before you even get to processing it. For example, using something such as :

[Key]
int Id {get; set;};

[Required]
string Name {get; set;}

[Required]
[EmailAddress]
string EmailAddress {get; set;}

Looking at the above we can see 3 fields. The first is the Key - this is used as the key value for the model. 

Then we have a string for name. This is a required field, so will validate if it is input and if the value is a string.

The first value is also required, but also has another valuation for EmailAddress. This will validate if the string is in the format for an email - such as sean@seanmccammon.com.

Limiting the length

Using these valiations can save you a lot of time. You can use validation techniques such as Regex to build more complex string validation (something for a future post). 

What you can also do, and the subject of this article, is validate not just on the type - but also the size.

For example, you may need to put a maximum length for a string such as name. You may want to limit the maximum size of the name to 80 characters. 

To do this you could do the following

[Required]
[StringLength(80)]
string name {get; set;}

The validator [StringLength(80)] on this string tells the system that the maximum length for the string is 80 characters. If a string input is greater than this - then the validation fails.

Setting minimum and maximum values for a string

What if you want to set the validation to a string that must have a minimum of 3 characters, but a maximum of 80.

This can also be done in a similar way using the StringLength command.

[Required]
[StringLength(80, MinimumLength = 6)]
string name {get; set;}

When a string is input; using the above it validates that the string is between 6 and 80 characters long.

Taking this one further - and using the StringLength validator to it's fullest - you can setup the error message the user will see if validation fails.

[Required]
[StringLength(80, MinimumLength = 6, ErrorMessage = "Name length much be between 6 and 80 characters in length.")]
string name {get; set;}

Adding this ErrorMessage gives the user a better idea of why the input will have failed. You can be more specific in these error messages - such as giving the format of the value that should be input - if you're using Regex to validate specific string formats.

In the case of StringLength we can le the user know that the string they have input for name is either too long or too short if it fails validation.

StringLength, MinimumLength and MaximumLength to validate length

Using the StringLength command along with MinimumLength and MaximumLength values means you can easily validate input in the model - to ensure it fits with the size of input you need.

Using StringLength in your model will illuminate the need to do some of the validation in your controller (although redundant validation will be needed if it's done in an API).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram