Range Validator in ASP.NET MVC tutorial with examples

Range Data Annotation Validator in ASP.NET MVC: A Guide with Code Example. ASP.NET MVC is a popular framework for building web applications. It provides a lot of features for validating user inputs. One such feature is the data annotation attribute, which is used to validate user inputs before saving them to the database.

In this article, we will take a look at the Range data annotation attribute and how to use it to validate user inputs in ASP.NET MVC.

What is the Range Data Annotation Attribute in ASP.NET MVC?

The Range data annotation attribute is used to specify the minimum and maximum values that can be entered into a field. This attribute is useful when you want to validate that a user has entered a value within a specific range. For example, if you want to validate that a user has entered an age between 18 and 60, you can use the Range data annotation attribute to do so.

How to use the Range Data Annotation Attribute in ASP.NET MVC?

To use the Range data annotation attribute in ASP.NET MVC, you need to add it to the property in your model class. Here is an example of how to use the Range data annotation attribute:
public class Person
{
    [Range(18, 60, ErrorMessage = "Age must be between 18 and 60")]
    public int Age { get; set; }
}
In the example above, we have defined a Person model class with an Age property. The Range data annotation attribute is added to the Age property and specifies the minimum and maximum values that can be entered. The ErrorMessage property is used to specify the error message that will be displayed if the user enters a value outside of the specified range.

Once the model class is defined, you can use it in your ASP.NET MVC view to display the form for the user to enter their age.
@model Person

@using (Html.BeginForm())
{
    @Html.LabelFor(m => m.Age)
    @Html.TextBoxFor(m => m.Age)
    @Html.ValidationMessageFor(m => m.Age)
    
}
    
Next Step: Enable client side validation, you can check this: Enable client side validation in ASP.NET MVC

In the example above, we have used the Html.BeginForm method to create a form. The Html.LabelFor method is used to display a label for the Age field, the Html.TextBoxFor method is used to display a textbox for the user to enter their age, and the Html.ValidationMessageFor method is used to display the error message if the user enters an invalid value.

When the user submits the form, ASP.NET MVC will automatically validate the input using the Range data annotation attribute. If the user enters a value outside of the specified range, the error message will be displayed next to the textbox.

Conclusion

In this article, we have looked at the Range data annotation attribute in ASP.NET MVC and how to use it to validate user inputs. By using this attribute, you can easily ensure that the user has entered a value within a specific range.
 
Best quality Asp .Net Ajax Control Toolkit tutorials.

Give your valuable comments.

Name
Email
Comment
2 + 3 =
 

About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice