Reads HTML constraints
Supports required fields, email inputs, minlength, maxlength, pattern, required checkboxes, radio groups, and checkbox groups.
Plugin overview
A11y Form Validator enhances existing labels, inputs, selects, textareas, checkboxes, radio groups, and fieldsets. It reads native constraints, supports custom rules, async validation, and server errors, renders accessible inline messages, can add a focusable summary and character counts, supports opt-in locale packs, and cleans up generated DOM when destroyed.
What it does
The plugin is designed as progressive enhancement: your form stays useful HTML, and JavaScript adds validation state, messages, focus handling, and optional addons.
Supports required fields, email inputs, minlength, maxlength, pattern, required checkboxes, radio groups, and checkbox groups.
Use data-validate, data-message-*, global messages, field
messages, or registered rules for product-specific validation.
Generated errors connect to fields with aria-describedby and
aria-errormessage, while preserving existing descriptions.
The summary addon creates a labelled, focusable section with links to invalid fields and can receive focus after blocked submits.
Controls with maxlength, minlength, or
data-character-count can expose a polite count message.
Apply field-level and form-level server errors with setErrors() after
failed sign-in, registration, checkout, or session checks.
Async custom rules support flows such as username availability or invite-code checks. Request cancellation remains the application responsibility.
Duplicate initialization returns the existing instance, and destroy()
removes listeners, generated errors, addon DOM, timers, classes, and ARIA state.
English messages stay in the main bundle. Other locale JSON files are opt-in and can be imported or supplied inline during initialization.
Quick start
Build the package before opening local demos. Demo pages import from
./dist/index.js and ./dist/styles.css; the direct browser demo
imports ./dist/index.min.js.
<link rel="stylesheet" href="./dist/styles.css" />
<form data-a11y-form-validator>
<label for="email">Email</label>
<input id="email" name="email" type="email" required />
<label for="message">Message</label>
<textarea
id="message"
name="message"
required
minlength="10"
maxlength="180"
data-character-count
></textarea>
<button type="submit">Send</button>
</form>
<script type="module">
import {
createDefaultPreset,
createFormValidator
} from "./dist/index.js";
const form = document.querySelector(
"[data-a11y-form-validator]"
);
if (form instanceof HTMLFormElement) {
createFormValidator(form, createDefaultPreset());
}
</script>
API at a glance
Use createDefaultPreset() when the page should include the error summary addon.
Use createNoSummaryPreset() for the same submit and blur validation pattern
without summary UI. Keep the returned instance when your app needs server errors, dynamic
fields, custom rules, or validation events.
createFormValidator(form, options)
Enhances one HTMLFormElement and returns the validator instance. Calling it
again on the same form returns the existing instance.
initFormValidators(options, root)
Finds every [data-a11y-form-validator] form under root and
initializes each one with the same options.
createDefaultPreset(), createNoSummaryPreset(), or createMinimalPreset()Default includes the error summary addon. No-summary keeps submit and blur validation without summary UI. Minimal validates on submit and focuses the first invalid field.
A11yFormValidatorDirect class construction is available for class-based integrations, but the factory is the simplest entry point for most pages.
validateOn chooses submit, blur, input, or change triggers.focusOnError sends focus to the summary, first invalid field, or nowhere.errorMode chooses inline, native, or both error presentation.addons accepts imported addon objects such as the error summary or character count helpers.validate() checks the whole form.validateField() checks one field by name or element.refresh() rescans fields after dynamic markup changes.getErrors() and getState() expose the current state.setErrors() renders field and form errors from your backend.clearErrors() removes rendered validation messages.focusOnError() moves focus according to your configured behavior.destroy() removes listeners, generated DOM, classes, and ARIA state.rules adds field-specific validation configuration.messages, locales, and locale customize error text.registerRule() and unregisterRule() manage custom rules.renderer replaces the default inline error rendering.Accessibility behavior
Inline errors are tied to controls with generated IDs, polite live regions, and
restored aria-describedby values when errors clear or the plugin is destroyed.
Depending on focusOnError, focus can move to the summary, the first invalid
field, or stay under application control.
Radio groups and checkbox groups sharing a name are treated as grouped
fields, with errors rendered outside option labels.
Tab moves through fields and summary links, Enter submits forms or activates links, and Space toggles native checkboxes and radio buttons.
Demo gallery
Each demo focuses on one behavior so the markup, rule setup, and validation state are easy to inspect.
Semantic contact form with inline errors, a summary, grouped choices, and character count guidance.
Same semantic form enhancement imported directly from the optional minified ESM file.
A small form using the minimal preset and native required, email, and minlength checks.
Data-attribute rules and messages for markup produced by content systems or templates.
data-validate and data-message-*.Labelled, focusable summary region with links that send users back to invalid fields.
Password confirmation with a custom same-as rule and custom error copy.
Switch between login and registration forms while only the visible form is active and validated.
Conditional billing validation plus required shipping and payment method choices.
Connect conditional sections with validation refresh, visible-only required fields, and summary updates.
Username availability checked with an async custom rule and debounced input validation.
admin or editor.Apply server-provided field and form messages after a failed request.
setErrors() and form-level messages.Compare English fallback, imported locale JSON, and inline locale messages.
Destroy and reinitialize the validator to swap runtime locale packs and regenerate errors.
destroy(), locale options, and generated DOM cleanup.Integration notes
npm run build before opening local demos because they import from dist.dist/index.min.js or a11y-form-validator/min.