Under The Hood
frontend htmx HTMX 1.x / 2.x

HTML5 Form Validation & htmx:validation:failed Event Hooks

Last updated
Prerequisites:
HTML5 Constraint Validation API (`checkValidity`, `reportValidity`)
HTMX Events
  • htmx
  • validation
  • html5
  • events
  • forms
  • htmx-validation

Read at your depth

The practical view

HTMX natively honors standard HTML5 form validation attributes (`required`, `pattern`, `minlength`, `type="email"`). When a form or button with `hx-post` is submitted, HTMX calls `form.checkValidity()`. If invalid, the AJAX request is blocked and the browser displays native error bubbles. You can listen to `htmx:validation:validate` or `htmx:validation:failed` in JavaScript to attach custom validation rules.

The same idea in other frameworks

react equivalent
React Hook Form / Zod resolver client validation.

Legacy vs modern

Heavy Client-Side Validation Library vs Native HTML5 + HTMX Event Hooks

Third-party validation bundles add 50KB of JS; HTMX utilizes native HTML5 validation with custom event hooks.

before → after
Custom JS Validation Engine
if (!validateRegex(email)) { showError(); return; } // ❌ Manual regex checks in JS
Native HTML5 Validation with HTMX Hook
<input type="email" required minlength="5" /> // ✅ NEW: Native constraint validation

Interview gotchas

Context

Interviewers test your knowledge of server-side-only validation workflows.

The mechanical answer

Add the standard HTML5 **`novalidate`** attribute to the `<form>` element (or `hx-validate="false"` on the element). This causes HTMX to skip `checkValidity()` checks and submit the form data to the server immediately, allowing server-side validation to return formatted error messages in HTML.

Trap

Without `novalidate`, the browser will prevent submission and show native tooltips, preventing the server from receiving the invalid input.

Under The Hood — a multi-depth technical interview hub.

Third-party licenses Press ⌘ K to search.