Back to Devexpress

Validation Overview

aspnetmvc-12309-components-data-editors-extensions-common-concepts-validation-validation-overview.md

latest2.6 KB
Original Source

Validation Overview

  • Dec 17, 2020
  • 2 minutes 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.

csharp
[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 ApproachServer or Client Side AvailabilitySupported ASP.NET MVC Versions
Model ValidationClient and ServerASP.NET MVC 2 and ASP.NET MVC 3
Unobtrusive Client ValidationClient and ServerASP.NET MVC 3 and higher
jQuery Client ValidationClientAll
Built-in ValidationClient and ServerAll