Validation Overview

  • 2 min to read

ASP.NET MVC empowers you with the ability to validate user input both on client and server sides.

Client-side validation is performed on the client's browser and allows you to immediately show/hide error messages according to the user input validity. The client-side validation validates the form before it is submitted: if the form is not valid, it won't be submitted.

Server-side validation occurs when a form is submitted to the server. Server-side validation allows you to protect your web application from malicious end-users who can bypass client-side validation.

The code sample below demonstrates how to validate a model class instance on the server side before adding it to the database (if you use model validation or unobtrusive client validation approaches). The AddNewClient(...) action method uses a ModelStateDictionary.IsValid property value to ensure that the model instance has no validation errors. A valid model class instance can be safely added to the database.

[HttpPost]
public ActionResult AddNewClient([ModelBinder(typeof(DevExpressEditorsBinder))] MyProject.Models.Clients item) {
    if (ModelState.IsValid) {
        db.Clients.Add(item);
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(item);
}

Validation Approaches

The table below contains information about validation approaches: their availability on the server or client side and supported ASP.NET MVC versions.

Validation Approach Server or Client Side Availability Supported ASP.NET MVC Versions
Model Validation Client and Server ASP.NET MVC 2 and ASP.NET MVC 3
Unobtrusive Client Validation Client and Server ASP.NET MVC 3 and higher
jQuery Client Validation Client All
Built-in Validation Client and Server All