fieldValidation()
Show an error after someone edits a field.
Try the example
Enter “hello”, then move out of the email field. An error appears after blur. Correct it to “hello@example.com” and the error clears as you type.
Start typing, then leave the field.
Add it to your project
- Copy the HTML. Place the markup inside the component where this behavior belongs. The preview’s visual classes are website styles; use your own design system.
- Connect the behavior. Copy the JavaScript tab and call its exported
mount(element)after your component’s elements exist. Replace simulated delays and data operations with your own application code. - Clean up. Call the returned cleanup when the component unmounts. It aborts the example’s listeners and releases the helper’s timers, observers or state.
API and lifecycle
Provide a field plus validate(value, field) and render(message, field). Validation starts after the first blur or explicit check(), then runs during correction. Return an error string or an empty string from validate.
- Interface
field, validate, render; check/reset/destroy- Lifecycle
- check() marks the field touched and returns whether it is valid. reset() clears its touched state and rendered error. destroy() removes the blur, input and composition listeners.
Accessibility and safety
Implementation notes
Always validate at submission and on the server. Keep values after failure.
The render callback owns visible error text, aria-invalid and aria-describedby. Validate on submit and on the server too; preserve entered values after failures.
What to check
Submit an empty field, correct it, and verify the error clears. Test keyboard navigation, screen-reader error association and text composition.