Lesson 15 of 40 Forms Beginner 35 min

Validation in ASP.NET Core

Learn how validation helps protect your ASP.NET Core application from incorrect or incomplete input. Good validation improves data quality, reduces errors, and gives users clearer guidance when they fill in forms.

Part 1: Why Validation Matters

Validation checks whether the data entered by a user meets the rules required by the application. Without validation, your system may store empty names, unrealistic ages, invalid email addresses, or other incorrect values.

Validation is important because it:

Part 2: Data Annotations

In ASP.NET Core MVC, one of the most common ways to define validation rules is by using data annotation attributes on model properties.

using System.ComponentModel.DataAnnotations;

public class Student
{
  public int Id { get; set; }

  [Required]
  public string Name { get; set; }

  [Required]
  public string Course { get; set; }

  [Range(16, 100)]
  public int Age { get; set; }

  [EmailAddress]
  public string Email { get; set; }
}

These attributes define rules such as:

Part 3: Common Validation Attributes

ASP.NET Core provides several useful validation attributes out of the box.

Attribute Purpose
[Required] Makes sure the field is not empty
[StringLength] Limits the length of text input
[Range] Restricts numeric values to a valid range
[EmailAddress] Checks email format
[Phone] Checks phone number format

These attributes make model validation much easier and more readable.

Part 4: Checking ModelState

After the form is submitted, the controller checks whether the model passed validation. This is done with ModelState.IsValid.

[HttpPost]
public IActionResult Create(Student student)
{
  if (ModelState.IsValid)
  {
    // Save the student record
    return RedirectToAction("Index");
  }

  return View(student);
}

If the model is valid, the application continues. If not, the form is shown again so the user can correct the errors.

Part 5: Showing Validation Messages in the View

Validation rules are most useful when the user can clearly see what went wrong. In Razor views, validation messages can be displayed next to input fields.

<div>
  <label asp-for="Name"></label>
  <input asp-for="Name" />
  <span asp-validation-for="Name"></span>
</div>

If the Name field is empty, the validation message will appear beside that field.

Part 6: Server-Side and Client-Side Validation

ASP.NET Core supports both server-side and client-side validation.

Server-side validation is essential because it cannot be bypassed simply by disabling browser scripts. Client-side validation improves convenience and gives faster feedback to the user.

Part 7: Validation in the Student Project

In your Student CRUD application, validation ensures that:

This prevents incomplete or incorrect records from entering the system.

Part 8: Best Practices

Good validation is not only about protecting the database. It also makes the application feel more professional and trustworthy.

Summary

Validation is a key part of building reliable ASP.NET Core applications. By using data annotations, checking ModelState, and showing clear feedback in the view, you can prevent bad input and create a better user experience.

VISUAL STUDIO 2026 MADE EASY
Recommended Book

VISUAL STUDIO 2026 MADE EASY

Build real applications with C#, VB.NET, Python, JavaScript, C++, and .NET 10. A practical companion for mastering Visual Studio 2026 step by step.