StringLength Validator in ASP.NET MVC tutorial with examples

StringLength Data Annotation Validator in ASP.NET MVC: A Tutorial with Code Example. One of the most common data annotations is the StringLength attribute. This attribute is used to ensure that a string property of a model is within a specific length range. In this blog post, we'll explore the StringLength data annotation and how to use it in ASP.NET MVC.

What is the StringLength Data Annotation?


The StringLength data annotation is a built-in validation attribute in ASP.NET MVC that allows you to limit the length of a string property of a model. It specifies the maximum and minimum lengths that a string property can have. This helps ensure that user input is within the expected range, preventing errors and unexpected behavior.

The syntax of the StringLength data annotation is as follows:
[StringLength(MaximumLength: int, MinimumLength = int)]
The MaximumLength parameter specifies the maximum length that the string property can have, while the MinimumLength parameter specifies the minimum length that the string property can have. Both parameters are optional, but if you specify only the MaximumLength parameter, the minimum length is assumed to be zero.

How to Use the StringLength Data Annotation


To use the StringLength data annotation, you first need to add it to the string property of the model that you want to validate. Here's an example:
public class Person
{
    [StringLength(50)]
    public string Name { get; set; }
}
In the above example, we're using the StringLength data annotation to ensure that the Name property of the Person class is no more than 50 characters long.

If you want to set a minimum length for the string property, you can add the MinimumLength parameter to the data annotation, like this:
public class Person
{
    [StringLength(50, MinimumLength = 2)]
    public string Name { get; set; }
}
In the above example, we're using the StringLength data annotation to ensure that the Name property of the Person class is between 2 and 50 characters long.

Handling Errors


If the user input violates the StringLength data annotation, ASP.NET MVC will add a validation error to the ModelState object. You can then check for errors and display them to the user in the view.

Next Step: Enable client side validation, you can check this: Enable client side validation in ASP.NET MVC

Here's an example of how to check for errors in the view:
@if (ViewData.ModelState["Name"].Errors.Count > 0)
{
    
@Html.ValidationMessageFor(m => m.Name)
}
In the above example, we're checking if there are any errors for the Name property of the model. If there are, we're displaying the validation error message to the user.

Conclusion


The StringLength data annotation is a simple but powerful way to perform data validation on string properties of a model in ASP.NET MVC. By specifying the maximum and minimum length of a string property, you can ensure that user input is within the expected range and prevent errors and unexpected behavior.

While the StringLength data annotation is just one of many data annotations available in ASP.NET MVC, it's an important one to keep in mind as you build web applications that rely on user input. By validating user input, you can ensure the integrity and security of your application and provide a better user experience.
 
We are nothing without our users ! You can help us offer even more high quality content. Please share our page !
Best quality Asp .Net Ajax Control Toolkit tutorials.

Give your valuable comments.

Name
Email
Comment
1 + 2 =
 
About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2023 CodingFusion
50+ C# Programs for beginners to practice