liveBook · Manning

Client-side validation

As explained in Pro ASP.NET Core 7, Tenth Edition

Client-side validation is the technique of checking user-entered data in the client environment before the form data is sent to the server.

Client-side validation checks form values with JavaScript before sending them to the server. It gives users immediate feedback instead of making them wait for a request, server processing, and a response to discover a missing or invalid value. It complements server-side validation rather than replacing it. section 29.6 section 9.3.6

In ASP.NET Core, form tag helpers emit data-val* attributes describing field constraints. Microsoft's unobtrusive validation script reads those attributes and configures jQuery Validation, which performs the checks. The rules therefore appear in generated HTML rather than in a separate hand-written check for every input. section 29.6

To enable the supplied setup:

  • Install jquery-validate and jquery-validation-unobtrusive with LibMan; install core jQuery too if it is not already present. section 29.6
  • Load jQuery, jQuery Validation, and the unobtrusive script in that order. The shown _Validation.cshtml also adds Bootstrap’s is-invalid class to inputs already marked input-validation-error. section 29.6
Listing 29.24 Adding elements in the _Validation.cshtml file in the Views/Shared folder
<script type="text/javascript">
    window.addEventListener("DOMContentLoaded", () => {
        document.querySelectorAll("input.input-validation-error")
            .forEach((elem) => { elem.classList.add("is-invalid"); }
        );
    });
</script>
<script src="/lib/jquery/jquery.min.js"></script>
<script src="/lib/jquery-validate/jquery.validate.min.js"></script>
<script src=
  "/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js">
</script>

The shared _Validation.cshtml listing shows the script order and the small error-styling addition. section 29.6

A required field in the generated form

An input for Name can carry data-val="true" and data-val-required="Please enter a value". With the validation scripts loaded, those attributes provide the client-side rule. A checkout form submitted empty illustrates the slower server-side alternative: errors appear only after ASP.NET Core processes the submission. section 29.6 section 9.3.6

Keep authentication on the server. Checking credentials in the client would mean exposing valid credentials for comparison or trusting the client's claim that authentication succeeded. section 11.4

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Explains generated validation attributes and library roles, then gives package and script setup; custom client-side extension is deferred.
Examples
Uses a checkout form, a generated required-field input, LibMan commands, and a shared Razor view containing validation scripts.
What it assumes
Assumes an ASP.NET Core form and existing jQuery installation, while explaining how the validation scripts and generated attributes fit together.
What you can do after
Apply it via ASP.NET Core form tag helpers and the jQuery validation scripts to check form values before submission.
How it connects
Connects model validation, form tag helpers, validation attributes, jQuery Validation, server-side validation, and authentication.

Also explained in

Compare how 8 books explain client-side validation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.