Loading

ASP.NET MVC

What is Required and StringLength Attribute in MVC?. The Complete ASP.NET MVC Developer Course 2022 [Videos].

In this Video, I am going to discuss Required and StringLength Attribute in ASP.NET MVC Application with Examples. Please watch the Introduction of Data Annotation Video before proceeding to this Video as we are going to use the same example that we created in our previous Video. In our previous Video, we discuss the basics of data annotation attributes in the ASP.NET MVC application and discussed how to enable client-side validation in ASP.NET MVC Application. So lets discuss the built-in Data Annotation Attributes that are available in ASP.NET MVC for model validation one by one.

Required Attribute in ASP.NET MVC:

lets understand Required Attribute with one example. Our business requirement is that the First name and Last Name of an employee cant be empty. That means we will force the Employee to give the first name and last name; we can achieve this very easily in the ASP.NET MVC application by decorating the FirstName and LastName properties of the Employee Model with the Required data annotation attribute as shown below. The Required attribute makes the model property as required.

[Required]
public string FirstName
{
get;
set;
}
[Required]
public string LastName
{
get;
set;
}

The Required attribute raises a validation error, if either the property value is null or empty. The built-in required validation attributes provide both client-side and server-side validation like other built-in validation attributes. When we submit the page without providing the FirstName and LastName of an employee, we will get the error message as shown in the below image.

Required Attribute in ASP.NET MVC Application

With the required attributes in place, if someone tries to submit the page without providing the FirstName and LastName values, then it will give you the default error as shown in the above image. But if you want to provide some user-defined error message when validation fails, then you can use the other overloaded version of the Required attribute which accepts ErrorMessage as an input parameter as shown below.

[Required(ErrorMessage = "First Name is Required")]
public string FirstName
{
get;
set;
}
[Required(ErrorMessage = "Last Name is Required")]
public string LastName
{
get;
set;
}

With the above changes, if someone tries to submit the page without providing the FirstName and LastName values, then it will give the user-defined error message as shown in the below image.

Required Attribute in ASP.NET MVC Application

StringLength Attribute in ASP.NET MVC

In our last example, we are forcing the user to enter his first name and last but what happens if he enters a name with enormous length? For example, our business requirement is that the employees LastName should not be greater than 30 characters that mean we need to set a maximum of 30 characters that can be entered for the employee the last name. We can achieve this very easily using the StringLength data annotation attribute in the ASP.NET MVC application. To achieve this we need to decorate the LastName property with the StringLength attribute as shown below.

[Required(ErrorMessage = "Last Name is Required")]
[StringLength(30)]
public string LastName
{
get;
set;
}

When we run the application, and when we enter more than 30 characters in the LastName textbox, then we will get the below default error message:

StringLength Attribute in ASP.NET MVC Application

Note: We can apply multiple validation attributes on a single property. The MinimumLength is an optional named parameter that is used to specify the minimum length for a string. We can also specify the user-defined error message as shown below. Here, we specify the minimum length as 4 and the ErrorMessage as the Last name should be between 4 and 30 characters.

[Required(ErrorMessage = "Last Name is Required")]
[StringLength(30, MinimumLength = 4,
ErrorMessage = "Last name should be between 4 and 30 characters")]
public string LastName
{
get;
set;
}

In the above example, we have decorated the LastName” property with the StringLength attribute and then specified the Minimum and Maximum length of the model properties. We also used the [Required] attribute. So, at this point, the LastName property is required and should be between 4 and 30 characters.

Points to remember about StringLength Attribute in MVC:
  1. [StringLength] attribute is present in System.ComponentModel.DataAnnotations namespace.
  2. [StringLength] attribute verifies that a string is of a certain length, but does not enforce that the property is REQUIRED. If you want to enforce that the property is required then use the [Required] attribute.

Now run the application and check everything is working as expected as shown below.

StringLength Attribute in ASP.NET MVC Application

See All

Comments (299 Comments)

Submit Your Comment

See All Posts

Related Posts

ASP.NET MVC / Youtube

What is MVC?

MVC is an architectural software design pattern that is used for developing interactive applications where their user interaction is involved and based on the user interaction some event handling has occurred. It is not only used for web-based applications but it can also be used for Desktop or mobile-based applications where there are user interactions involved.
28-jan-2022 /28 /299

ASP.NET MVC / Youtube

How to Creat First ASP.NET MVC Application using Visual Studio?

In this article, I am going to discuss how to create the first ASP.NET MVC Application step by step from scratch using Visual Studio 2015. You can use any version as per your choice but the step will remain the same. Please read our previous article before proceeding to this article where we gave a brief introduction to ASP.NET MVC Framework.
28-jan-2022 /28 /299

ASP.NET MVC / Youtube

What is ASP.NET MVC File and Folder Structure?

In this article, I am going to discuss the auto-generated ASP.NET MVC File and File Structure when we create a new ASP.NET MVC application. Please read our previous article before proceeding to this article where we discussed how to create ASP.NET MVC 5 application step by step from scratch.
28-jan-2022 /28 /299