Range Attribute in ASP.NET MVC

Range Attribute in ASP.NET MVC Application

In this article, I am going to discuss Range Attribute in ASP.NET MVC Application along with MinLength and MaxLength Attribute with Examples. Please read the following articles before proceeding to this article as I am going to use the same example that we started in our previous article.

Data Annotations in ASP.NET MVC

Required and StringLength Attribute

Regular Expression Attribute

Range Attribute in ASP.NET MVC Application:

Notice that we don’t have validation on Age field. That means if you enter 5000 as the age and click on the Save button, then the data also gets saved. As we know an employee having 5000 years as the age is not possible. So, let’s validate the Age field, and enforce users to enter a value between 25 and 60. We can achieve this very easily by using the RangeAttribute in ASP.NET MVC Application. The Range attributes specify the minimum and maximum constraints for a numerical number, as shown below:

[Range(25, 60, ErrorMessage = "Age must be between 25 and 60")]
public int Age
{
    get;
    set;
}

In the above example, we set the age of the employee will be between 25 to 60 years to pass the validation. Here, the first parameter of the attribute is the minimum value and the second parameter is the maximum value and the third parameter is the error message that we want to show when the validation failed.

When a user tries to enter the age which is not between 25 and 60 and click on the submit button, then he will get the validation error message as shown below:

Range Attribute in ASP.NET MVC

At this point, we should not be able to enter any values outside the range of 25 and 60 for the Age field. The Range attribute in ASP.NET MVC can also be used to validate the DateTime fields.

Range Attribute with DateTime fields in ASP.NET MVC Application.

Notice that, we are passing DateTime as the type and specifying the minimum and maximum values for DateOfBirth. We are also using the DisplayFormat attribute so that the only date part of DateTime is displayed in the view.

[Range(typeof(DateTime), "01-01-1970", "01-01-2005",
                    ErrorMessage = "Date of Birth Must be between 01-01-1970 and 01-01-2005")]
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
public DateTime DateOfBirth
{
    get;
    set;
}

At this point, we should not be able to enter any values outside the range of “01/01/1970” and “01/01/2005” for the DateOfBirth field. However, when the Range attribute is used with DateTime fields, the client-side validation does not work as expected. We will discuss this in a later session. 

MinLength and MaxLength Attribute in ASP.NET MVC Application

These two attributes are used to specify the Minimum Length and Maximum Length of a property. For example, if we want to restrict the Employee Address as 5 as the minimum length and 25 as the maximum length then we can decorate the address property with the MinLength and Maxlength attribute as shown below

[MinLength(5, ErrorMessage = "The Address must be at least 5 characters")]
[MaxLength(25, ErrorMessage = "The Address cannot be more than 25 characters")]
public string Address
{
    get;
    set;
}

When we submit the page by entering more than 25 characters, it will give the error as shown below.

Range Attribute in ASP.NET MVC

In the next article, I am going to discuss Creating Custom validation Attributes in ASP.NET MVC. Here, in this article, I try to explain the Range Attribute in ASP.NET MVC application with Examples. I hope this Range Attribute in the ASP.NET MVC application article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this Range Attribute in the MVC application article. 

2 thoughts on “Range Attribute in ASP.NET MVC”

Leave a Reply

Your email address will not be published.