---
title: "Explain Range data annotation for validation."  
description: "Explain Range data annotation for validation."  
author: "Utpal Vishwas"  
published: 2023-10-20  
updated: 2023-10-22  
canonical: https://www.mindstick.com/forum/160243/explain-range-data-annotation-for-validation  
category: ".net core"  
tags: ["asp.net core", ".net core", "data annotations"]  
reading_time: 2 minutes  

---

# Explain Range data annotation for validation.

[Explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) [Range](https://www.mindstick.com/articles/23243/complete-troubleshooting-tips-for-belkin-n300-range-extender-setup) [data annotation](https://www.mindstick.com/articles/12234/validation-using-data-annotation-using-entity-framework) for validation.

## Replies

### Reply by Aryan Kumar

The **Range** [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) annotation in .NET Core is used for validation to ensure that a numeric property falls within a specified range of values. It is primarily applied to properties that represent numbers, such as integers, decimals, or floating-point numbers. The **Range** data annotation allows you to specify the minimum and maximum values for a property. Here's how to use it:

## Import the Required Namespace:

Make sure to import the **System.ComponentModel.DataAnnotations** namespace, which contains the **Range** attribute.

```plaintext
using System.ComponentModel.DataAnnotations;
```

## Apply the Range Attribute:

Apply the **Range** attribute to the numeric property in your model class. You can set the **Minimum** and **Maximum** properties to define the acceptable range for the property.

```plaintext
public class Product
{
    [Range(1, 100, ErrorMessage = "The price must be between $1 and $100.")]
    public decimal Price { get; set; }
}
```

In this example, the **Price** property is decorated with the **Range** attribute, specifying that the minimum value is 1 and the maximum value is 100. The **ErrorMessage** property is also set to provide a custom error message if the validation fails.

## Display Validation Errors in Views:

In your views, you can use the **ValidationMessageFor** HTML helper to display validation errors for the property.

```plaintext
<div class="form-group">
    <label asp-for="Price"></label>
    <input asp-for="Price" class="form-control" />
    <span asp-validation-for="Price" class="text-danger"></span>
</div>
```

This code will display the custom error message defined in the **Range** attribute when the numeric value is outside the specified range.

## Server-Side Validation:

In your controller action methods, you can check the **ModelState.IsValid** property to determine if the validation has passed. If **ModelState.IsValid** is **false**, you can handle validation errors as needed.

```plaintext
[HttpPost]
public IActionResult Create(Product product)
{
    if (!ModelState.IsValid)
    {
        // Handle validation errors, e.g., return to the form with error messages.
        return View(product);
    }

    // If validation is successful, proceed with the data.
    // ...
}
```

The **Range** data annotation is a powerful tool for validating that numeric properties in your model fall within a specified range of values. It provides a straightforward way to set up numeric validation rules with customizable error messages, improving data integrity and the user experience.


---

Original Source: https://www.mindstick.com/forum/160243/explain-range-data-annotation-for-validation

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
