Categories: .Net Core 3C#

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 third 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.stackstaging.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).

Sean

Share
Published by
Sean
Tags: c#

Recent Posts

The Question: Cream or Jam First

So, to set the scene. I'm currently away on a break. I was out walking…

1 year ago

Creating a WordPress SEO Plugin From Scratch

Creating a WordPress SEO plugin from scratch is an ambitious yet rewarding endeavor that can…

1 year ago

A Step-by-Step Guide to Building an API Endpoint with Java Spring Boot

Introduction to API Endpoint Development Understanding API Endpoints API endpoints serve as the communicative bridges…

2 years ago

The Fine Line Between Knowledge and Overwhelm: Signs You're Learning Too Much

The innate human desire to learn and grow is a fundamental part of our nature.…

2 years ago

Demystifying the @Transactional Annotation in Spring Boot

When working within the Spring framework, developers often encounter scenarios where transactional integrity is crucial…

2 years ago

Demystifying @Autowired in Java Spring Boot: A Beginner's Guide

Introduction to @Autowired in Java Spring Boot What is Dependency Injection? Dependency Injection (DI) is…

2 years ago