Interactive Example · Input feedback
Inline validation feedback for a text field
An email field that checks itself when you leave it, and shows a problem with an outline, an icon and a sentence that says how to fix it.
Written by Inspireca Design (drafted with AI assistance)Updated Not yet reviewed by an editor
Try it
Use it with a mouse, by touch or with the keyboard. The controls below show its other states.
How it works
Good validation feels like help; bad validation feels like being told off while you are still typing. The difference is mostly timing and wording.
When it checks
The field checks when you leave it or submit, not on every key press. Showing "invalid email" after the first letter is correct and useless. Once an error is showing, it clears as soon as you start typing again, so the fix is acknowledged immediately.
What changes
- The outline turns red and an alert icon appears at the end of the field.
- The help text under the field is replaced by a sentence saying what to do.
- The field nudges a few pixels left and right once, over about a quarter of a second, to draw the eye.
- When the value is fine, a green tick pops in and the text says so.
Try it: type mira@example and press Tab. Then use the State control to see each state on its own.
Reserve the space
The line under the field always has room for one line of text. Without that, every error would push the rest of the form down. The Headspace breakdown shows field states on real screens, and the error message prompt helps with the wording.
Accessibility notes
- Three signals, not one. Colour, icon and words together, so the error does not depend on seeing red (WCAG 2.2, 1.4.1 and 3.3.1 Error Identification).
- The message says how to fix it (3.3.3 Error Suggestion).
- Tied to the field.
aria-invalidmarks the field andaria-describedbypoints at the message, which sits in a polite live region so it is read out when it changes. - The label is a real label above the field and stays visible; the placeholder is only an example.
- Reduced motion: no nudge and no pop; the outline, icon and text still change.
- Text size 16 px in the field, which also stops iOS Safari from zooming in on focus.
The code
This is the source of the preview above: React with Tailwind CSS classes (input-validation-feedback.tsx). Colour names such as bg-primary and the ex- animation classes come from this site’s own stylesheet; replace them with your own.
"use client";
import { useId, useState } from "react";
import { AlertCircle, CheckCircle2 } from "lucide-react";
import type { DemoProps } from "@/lib/examples/keys";
const looksLikeEmail = (text: string) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(text);
/**
* An email field that checks itself when you leave it, not while you are still typing.
* A problem is shown three ways: a red outline, an icon, and a sentence that says how to fix it.
* The field nudges sideways once to draw the eye; with reduced motion it simply does not move.
*/
export default function InputValidationFeedback({ values }: DemoProps) {
const forced = String(values.state);
const id = useId();
const [text, setText] = useState("");
const [checked, setChecked] = useState(false);
const [nudge, setNudge] = useState(0); // changing the key replays the nudge
const value = forced === "error" ? "mira@example" : forced === "valid" ? "mira@example.com" : text;
const status = forced === "interactive" ? (checked ? (looksLikeEmail(text) ? "valid" : "error") : "idle") : forced;
const check = () => { setChecked(true); if (!looksLikeEmail(text)) setNudge((n) => n + 1); };
return (
<form noValidate onSubmit={(e) => { e.preventDefault(); check(); }} className="w-full max-w-xs text-left">
<label htmlFor={id} className="text-sm font-medium">Email address</label>
<div key={nudge} className={`relative mt-1.5 ${status === "error" ? "ex-nudge" : ""}`}>
<input
id={id}
type="email"
inputMode="email"
autoComplete="off"
value={value}
readOnly={forced !== "interactive"}
onChange={(e) => { setText(e.target.value); setChecked(false); }}
onBlur={() => { if (text) check(); }}
aria-invalid={status === "error" || undefined}
aria-describedby={`${id}-help`}
placeholder="name@example.com"
className={`h-12 w-full rounded-[10px] border bg-background px-3.5 pr-10 text-base outline-none transition-colors duration-150 placeholder:text-subtle-foreground focus-visible:ring-2 focus-visible:ring-offset-1 motion-reduce:transition-none ${
status === "error" ? "border-[#b42318] focus-visible:ring-[#b42318]" : status === "valid" ? "border-[#17663a] focus-visible:ring-[#17663a]" : "border-border-strong focus-visible:ring-foreground"}`}
/>
{status === "error" && <AlertCircle aria-hidden className="absolute right-3 top-3.5 size-5 text-[#b42318]" />}
{status === "valid" && <CheckCircle2 aria-hidden className="ex-pop absolute right-3 top-3.5 size-5 text-[#17663a]" />}
</div>
{/* One place for help and errors, tied to the field, announced when it changes. */}
<p id={`${id}-help`} aria-live="polite" className={`mt-1.5 min-h-5 text-sm ${status === "error" ? "text-[#b42318]" : "text-muted-foreground"}`}>
{status === "error" ? "Add the part after the dot, for example .com" : status === "valid" ? "Looks good." : "We only use it to send your receipt."}
</p>
</form>
);
}
See it in real apps
Still screenshots from the Inspireca Design library of the kinds of screen where this interaction is used. A screenshot cannot show the motion itself.