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:
- Protects the quality of stored data
- Prevents obvious user mistakes
- Improves the user experience
- Reduces errors later in the application workflow
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.
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:
[Required]— the field must not be empty[Range]— the value must be within a valid range[EmailAddress]— the value must look like a valid email address
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.
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.
<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 runs on the server after form submission
- Client-side validation can show errors in the browser before submission
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:
- The student name is not left blank
- The course field is filled in
- The age is within a realistic range
- The email address is valid
This prevents incomplete or incorrect records from entering the system.
Part 8: Best Practices
- Always validate important input
- Use clear validation rules that match business needs
- Show understandable error messages to users
- Check
ModelState.IsValidbefore saving data - Combine validation with clean form design
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.