---
title: "How to use Data annotation to validate required fields?"  
description: "How to use Data annotation to validate required fields?"  
author: "Sandra Emily"  
published: 2023-10-20  
updated: 2023-10-22  
canonical: https://www.mindstick.com/forum/160242/how-to-use-data-annotation-to-validate-required-fields  
category: ".net core"  
tags: ["asp.net core", ".net core", "data annotations"]  
reading_time: 2 minutes  

---

# How to use Data annotation to validate required fields?

How to use [Data annotation](https://www.mindstick.com/articles/12234/validation-using-data-annotation-using-entity-framework) to [validate](https://www.mindstick.com/forum/1490/what-is-better-solution-for-validate-form-textboxs-value) [required](https://www.mindstick.com/forum/160269/what-is-required-data-annotation-how-does-it-affect-model-validation) [fields](https://www.mindstick.com/forum/159126/sql-query-for-all-tables-and-fields-in-an-oracle-db)?

## Replies

### Reply by Aryan Kumar

In ASP.NET Core, you can use the **[Required]** [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) annotation to validate that a field is required, which means it cannot be empty or null. This annotation is used to ensure that users provide values for specific properties in your model. Here's how to use the **[Required]** data annotation for field validation:

## Import the Required Namespace:

Ensure that you've imported the **System.ComponentModel.DataAnnotations** namespace, which contains the **[Required]** attribute.

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

## Apply the [Required] Attribute:

Apply the **[Required]** attribute to the property in your model class that you want to validate as required. For example, if you have a "Name" property that must be filled out:

```plaintext
public class Person
{
    [Required(ErrorMessage = "The name field is required.")]
    public string Name { get; set; }
}
```

In this example, the "Name" property is decorated with the **[Required]** attribute. The **ErrorMessage** property is set to provide a custom error message if the validation fails.

## Display Validation Errors in Views:

In your views, use the **ValidationMessageFor** HTML helper to display validation errors for the property. This will display the custom error message defined in the **[Required]** attribute when the "Name" field is empty.

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

## 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(Person person)
{
    if (!ModelState.IsValid)
    {
        // Handle validation errors, e.g., return to the form with error messages.
        return View(person);
    }

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

By using the **[Required]** data annotation, you can ensure that specific fields are required in your model, provide custom error messages when users don't provide a value, and improve the user experience by making sure important data is not missing.


---

Original Source: https://www.mindstick.com/forum/160242/how-to-use-data-annotation-to-validate-required-fields

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
