Range Attribute in ASP.NET MVC Application Data Structures and Algorithms Tutorials.Please post your feedback, question, or comments about this Required and StringLength Attribute in the MVC article. I hope this Required and StringLength Attribute in the ASP.NET MVC Application article will help you with your need. Here, in this article, I try to explain the Required and StringLength Attribute in the ASP.NET MVC application with examples. In the next article, I am going to discuss Regular Expression Attribute in ASP.NET MVC Application. Now run the application and check everything is working as expected as shown below. If you want to enforce that the property is required then use the attribute. attribute verifies that a string is of a certain length, but does not enforce that the property is REQUIRED.Points to remember about StringLength Attribute in MVC: So, at this point, the LastName property is required and should be between 4 and 30 characters. 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. ĮrrorMessage = "Last name should be between 4 and 30 characters")] Here, we specify the minimum length as 4 and the ErrorMessage as the Last name should be between 4 and 30 characters. We can also specify the user-defined error message as shown below. The MinimumLength is an optional named parameter that is used to specify the minimum length for a string. Note: We can apply multiple validation attributes on a single property.
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: To achieve this we need to decorate the LastName property with the StringLength attribute as shown below. We can achieve this very easily using the StringLength data annotation attribute in the ASP.NET MVC application.
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 employee’s 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. 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.
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. 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. 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. The built-in required validation attributes provide both client-side and server-side validation like other built-in validation attributes. The Required attribute raises a validation error, if either the property value is null or empty. The Required attribute makes the model property as required. 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. Our business requirement is that the First name and Last Name of an employee can’t be empty. Let’s understand Required Attribute with one example. These attributes provide Server-side validation as well as client-side validation. Note: Data annotations are the attributes that we can find in the namespace.