DocsForms & drafts
Get started

Search Subtly documentation

Search by a task, helper name or framework.

Forms & drafts

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.

Newsletter signup

A sample form. No email is sent.

Start typing, then leave the field.

Actual npm package · local demonstration

Add it to your project

  1. 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.
  2. 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.
  3. 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.