Skip to content

About

🧐 Yet another vanilla JS form validator

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

form-validator-js

Declarative form validation for vanilla TypeScript / JavaScript, driven by data- attributes on HTML form fields. Bring your own rendering β€” the library only manages validity state and error messages.

Status: 1.1.0.

When this fits, when it doesn't

This is the Parsley.js / jQuery Validate style: rules go in HTML, JS just wires up the engine. Good for:

  • Server-rendered apps with light JS (Rails/Hotwire, Django, Laravel, plain HTML + HTMX, Alpine).
  • No-build static sites.
  • Codebases that don't already have a form-state library.

If you're on a component framework with state-managed forms (React, Vue, Svelte, Solid), reach for react-hook-form + zod, @tanstack/form + valibot, or similar. Schema-first validation with full TypeScript inference is a better fit there.

Install

npm install @form-validator-js/core @form-validator-js/validators

@form-validator-js/validators declares core as a peerDependency pinned to the same version. They are versioned together.

Minimal example

<form id="signup">
  <input id="username" name="username" type="text"     data-validation="required;minLength(3)">
  <input id="password" name="password" type="password" data-validation="required;minLength(8)">
  <input id="confirm"  name="confirm"  type="password" data-validation="required;equalsTo(password)">
  <ul id="errors"></ul>
  <button>Sign up</button>
</form>
import { FormValidator } from '@form-validator-js/core';
import { required, minLength, equalsTo } from '@form-validator-js/validators';

const form = document.getElementById('signup') as HTMLFormElement;
const errorList = document.getElementById('errors') as HTMLUListElement;

new FormValidator({
  form,
  validatorDeclarations: {
    required:  { ...required,  errorMessage: 'This field is required.' },
    minLength: { ...minLength, errorMessage: 'Too short.' },
    equalsTo:  { ...equalsTo,  errorMessage: 'Passwords must match.' },
  },
  onErrorMessageListChanged(element, messages) {
    // Called whenever an element's error-message list changes.
    // Render however you like; here we dump them into a single list.
    // Cross-field: typing in `password` triggers re-validation of `confirm`
    // through equalsTo's observable wiring.
    if (element === form) return;
    errorList.innerHTML = messages.map((m) => `<li>${m}</li>`).join('');
  },
});

The form gets novalidate set automatically, so the browser's built-in validation UI is suppressed.

Validation runs:

  • Per field β€” on input and/or focusout, depending on the trigger option. Default is 'blur-then-input': validate on focusout until a field has been shown an error, then eagerly on input. Cross-field reactivity (e.g. password change re-validating confirm via equalsTo) follows the same rule.
  • On submit β€” every field is validated. If any validator returns isValid: false, the submit is preventDefault'd.
  • On reset β€” every error is cleared.

Inputs linked to the form via the form="formId" attribute (outside the form element in the DOM) are also picked up β€” both for validation and for submit blocking.

Construct FormValidator before attaching other submit listeners on the same form. On an invalid submit, the validator calls stopImmediatePropagation so other submit handlers (analytics, save, integrations) don't observe the failed attempt. DOM listener order is registration order on the target β€” listeners attached before new FormValidator(...) will still fire. Register the validator first.

Project structure

packages/core/        Engine. FormValidator, result types, the validator contract.
packages/validators/  Ten built-in validators (required, minLength, …, step).

Development

npm install
npm run lint        # ESLint flat config + typescript-eslint
npm run typecheck   # tsc --noEmit
npm test            # Vitest (jsdom)
npm run build       # tsup, emits dist/index.{cjs,mjs,d.ts,d.mts}

CI runs lint β†’ typecheck β†’ coverage β†’ build on Node 24.

License

MIT β€” see LICENSE.

About

🧐 Yet another vanilla JS form validator

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages