Range Attribute in ASP.NET MVC

Range Attribute in ASP.NET MVC

In this article, I am going to discuss Range Attribute along with MinLength and MaxLength Attribute with some real time examples in ASP.NET MVC.

Please Read the below 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:

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 by using the RangeAttribute in ASP.NET MVC.

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, 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.

When a user tries to enter the age which is not between 25 and 60, and when click on the submit button, then he will get 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 Age field.

Range attribute in ASP.NET MVC can also be used to validate the DateTime fields.

Let’s understand Range attribute with DateTime fields.

Notice that, we are passing DateTime as the type and specifying the minimum and maximum values for DateOfBirth. We are also using 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 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

These two attributes are used to specify the Min Length and MaxLength 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 will discuss Creating Custom validation Attribute in ASP.NET MVC.

SUMMARY

In this article, I try to explain the Range Attribute in ASP.NET MVC application step by step with some real-time examples. I hope this article will help you with your need. I would like to have your feedback. Please post your feedback, question, or comments about this article. 

Leave a Reply

Your email address will not be published. Required fields are marked *