| 1 | --- |
| 2 | includeMockServer: true |
| 3 | title: Active Validation |
| 4 | description: Validate form input as you type |
| 5 | icon: "icon-[mdi--check]" |
| 6 | --- |
| 7 | |
| 8 | <script> |
| 9 | server.get("/demo", () => |
| 10 | `<div class="w-full max-w-sm mx-auto starting:opacity-0 transition duration-300"> |
| 11 | <form class="flex flex-col gap-1" autocomplete="off"> |
| 12 | <div class="mb-6"> |
| 13 | <label for="username" class="block text-xs font-medium uppercase tracking-wide text-neutral-500 dark:text-neutral-400 mb-3">Username</label> |
| 14 | <div class="relative"> |
| 15 | <input name="username" id="username" class="block w-full px-3 py-2.5 text-sm border border-neutral-200 dark:border-neutral-700 rounded-md bg-white dark:bg-neutral-900 text-neutral-800 dark:text-neutral-200 outline-none transition-shadow focus:border-neutral-400 dark:focus:border-neutral-500" placeholder="e.g. johndoe" |
| 16 | hx-post="/check-username" |
| 17 | hx-trigger="input changed delay:300ms" |
| 18 | hx-target="#username-msg" |
| 19 | hx-swap="innerMorph" |
| 20 | autocomplete="new-password" data-1p-ignore> |
| 21 | <div id="username-msg" class="absolute right-0 -bottom-6.5 text-right"></div> |
| 22 | </div> |
| 23 | </div> |
| 24 | <div class="mb-6"> |
| 25 | <label for="password" class="block text-xs font-medium uppercase tracking-wide text-neutral-500 dark:text-neutral-400 mb-3">Password</label> |
| 26 | <div class="relative"> |
| 27 | <input name="password" id="password" type="password" class="block w-full px-3 py-2.5 text-sm border border-neutral-200 dark:border-neutral-700 rounded-md bg-white dark:bg-neutral-900 text-neutral-800 dark:text-neutral-200 outline-none transition-shadow focus:border-neutral-400 dark:focus:border-neutral-500" placeholder="••••••••" |
| 28 | hx-post="/check-password" |
| 29 | hx-trigger="input changed delay:200ms" |
| 30 | hx-target="#password-msg" |
| 31 | hx-swap="innerMorph" |
| 32 | autocomplete="new-password" data-1p-ignore> |
| 33 | <div id="password-msg" class="absolute right-0 -bottom-6.5 text-right"></div> |
| 34 | </div> |
| 35 | </div> |
| 36 | <button type="button" onclick="this.closest('form').reset(); document.getElementById('username-msg').innerHTML=''; document.getElementById('password-msg').innerHTML='';" class="w-full mt-1 px-5 py-2.5 text-sm font-medium rounded-md text-white dark:text-neutral-900 bg-neutral-800 dark:bg-neutral-200 cursor-pointer hover:bg-neutral-700 dark:hover:bg-neutral-300 active:scale-[0.98] transition">Sign up</button> |
| 37 | </form> |
| 38 | </div>`); |
| 39 | |
| 40 | server.post(/\/check-username.*/, (req) => { |
| 41 | const taken = ["admin", "htmx", "test", "user", "root", "moderator"]; |
| 42 | const raw = (req.params.username || '').trim(); |
| 43 | const name = raw.toLowerCase(); |
| 44 | if (!name) return '<span></span>'; |
| 45 | if (name.length < 3) return `<span class="text-[0.675rem] starting:opacity-0 transition-opacity duration-200 text-red-600 dark:text-red-400">Must be at least 3 characters.</span>`; |
| 46 | if (!/^[a-z0-9_]+$/.test(name)) return `<span class="text-[0.675rem] starting:opacity-0 transition-opacity duration-200 text-red-600 dark:text-red-400">Only letters, numbers, and underscores.</span>`; |
| 47 | if (taken.includes(name)) return `<span class="text-[0.675rem] starting:opacity-0 transition-opacity duration-200 text-red-600 dark:text-red-400">"${raw}" is already taken.</span>`; |
| 48 | return { delay: 150, body: `<span class="text-[0.675rem] starting:opacity-0 transition-opacity duration-200 text-green-700 dark:text-green-400">${raw} is available!</span>` }; |
| 49 | }); |
| 50 | |
| 51 | server.post(/\/check-password.*/, (req) => { |
| 52 | const pw = req.params.password || ''; |
| 53 | if (!pw) return '<span></span>'; |
| 54 | if (pw.length < 6) return `<span class="text-[0.675rem] starting:opacity-0 transition-opacity duration-200 text-red-600 dark:text-red-400">Too short.</span>`; |
| 55 | if (pw.length < 10) return `<span class="text-[0.675rem] starting:opacity-0 transition-opacity duration-200 text-amber-600 dark:text-amber-400">Decent password.</span>`; |
| 56 | return `<span class="text-[0.675rem] starting:opacity-0 transition-opacity duration-200 text-green-700 dark:text-green-400">Strong password.</span>`; |
| 57 | }); |
| 58 | |
| 59 | server.start("/demo"); |
| 60 | </script> |
| 61 | |
| 62 | <div id="demo-content" class="not-prose demo-container flex items-center justify-center min-h-[400px]"></div> |
| 63 | |
| 64 | ## Basic usage |
| 65 | |
| 66 | On the client, the input validates on each keystroke (debounced). |
| 67 | |
| 68 | ```html |
| 69 | <input name="username" |
| 70 | hx-post="/check-username" |
| 71 | hx-trigger="input changed delay:300ms" |
| 72 | hx-target="next span"> |
| 73 | |
| 74 | <span></span> |
| 75 | ``` |
| 76 | |
| 77 | - [`hx-post`](/reference/attributes/hx-post) sends the value to `/check-username`. |
| 78 | - [`hx-trigger`](/reference/attributes/hx-trigger) fires on [`input`](https://developer.mozilla.org/en-US/docs/Web/API/Element/input_event) after a 300ms [`delay`](/reference/attributes/hx-trigger#delay), and only when the value has [`changed`](/reference/attributes/hx-trigger#changed). |
| 79 | - [`hx-target`](/reference/attributes/hx-target)=[`"next span"`](/reference/attributes/hx-target#relative-targets) puts the response into the next sibling `<span>`. |
| 80 | |
| 81 | On the server, respond with a validation message: |
| 82 | |
| 83 | ```html |
| 84 | <span class="error">That username is taken.</span> |
| 85 | ``` |
| 86 | |
| 87 | Or an empty `<span>` when valid. |