liveBook · Manning

Client-side validation

As explained in ASP.NET Core in Action, 3e

Client-side validation is validation performed in client software, typically a browser, before submitted data reaches the server.

Client-side validation checks form values in the browser before they reach the server. It can show an error immediately—and prevent an invalid form submission—so users can correct a field without waiting for a response. It is a convenience, not a security boundary: browser checks can be bypassed, so always validate submitted data on the server too. section 16.3.3 section 16.3.1

A Razor Pages form can get that feedback in two ways:

  • Use an HTML5 input type such as email. Modern browsers can reject an incorrectly formatted address before submission, though constraint-validation support varies by browser. section 16.3.3
  • Use JavaScript validation. DataAnnotations on a binding model give the Razor Input Tag Helper metadata for generating data-val-* attributes; jQuery Validation and unobtrusive validation scripts use those attributes to check the form. section 18.2.3 section 18.6

An email field

Decorate a C# email property with [EmailAddress], then write <input asp-for="Input.Email" /> in the Razor form. The Input Tag Helper generates an email-type input and validation attributes. With the validation scripts loaded, an invalid address can produce feedback before a request is sent. section 18.2.3 section 16.3.3

<input type="email" id="Input_Email" name="Input.Email"
   value="test@example.com" data-val="true"
   data-val-email="The Email Address field is not a valid e-mail address."
   Data-val-required="The Email Address field is required."
   />

Compare the short asp-for markup with the generated input: its email type and data-val-* attributes provide the browser and JavaScript with validation information. section 18.2.3

Keep the server check even when the form appears to work entirely in the browser. In Razor Pages, validation runs after model binding, and the page handler still runs when validation fails; check ModelState.IsValid and respond accordingly. Built-in DataAnnotations commonly gain client-side support through generated markup, but a custom ValidationAttribute runs only on the server by default. Giving it a browser-side check requires additional wiring, such as a jQuery Validation adapter. section 16.3.1 section 16.5 section 32.3

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains HTML5 checks, generated validation attributes, and required scripts; refers custom jQuery adapters to external documentation.
Examples
Uses an email input and its generated HTML to show validation before submission.
What it assumes
Builds the Tag Helper approach from a Razor Pages form and a DataAnnotations-decorated binding model.
What you can do after
Apply it via Razor Pages Input Tag Helpers and jQuery Validation without replacing server-side checks.
How it connects
Connects DataAnnotations and Input Tag Helpers to browser checks, while retaining ModelState and server-side validation.

Also explained in

Compare how 5 books explain client-side validation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.