Web component wrapper that adds additive validation to ensure two text-type fields match.
Additional demos:
npm install @aarongustafson/form-matching-fields
Import the package to automatically define the <form-matching-fields> custom element:
import '@aarongustafson/form-matching-fields';
Or use the define-only script in HTML:
<script
src="./node_modules/@aarongustafson/form-matching-fields/define.js"
type="module"
></script>
Import the class and define the custom element with your preferred tag name:
import { FormMatchingFieldsElement } from '@aarongustafson/form-matching-fields/form-matching-fields.js';
customElements.define('my-custom-name', FormMatchingFieldsElement);
<form-matching-fields>
<label for="password">Password</label>
<input id="password" type="password" required />
<label for="password-again">Password again</label>
<input id="password-again" type="password" required />
</form-matching-fields>
This component intentionally keeps matching behavior simple:
The component does not replace native or existing custom validation:
| Attribute | Type | Default | Description |
|---|---|---|---|
validation-message |
string |
The fields “{label_1}” and “{label_2}” should match |
Custom mismatch message template. Supports placeholder replacement for {label_1} and {label_2}. |
| Property | Type | Description |
|---|---|---|
validationMessage |
string |
Property equivalent of validation-message. |
When replacing {label_1} and {label_2}, labels are resolved in this order:
<label for> text<label> textaria-labelnameid<form-matching-fields>
<label for="email">Email</label>
<input id="email" type="email" required />
<label for="verify-email">Verify email</label>
<input id="verify-email" type="email" required />
</form-matching-fields>
<form-matching-fields
validation-message="Please make sure {label_2} matches {label_1}."
>
<label for="email">Email</label>
<input id="email" type="email" required />
<label for="verify-email">Verify email</label>
<input id="verify-email" type="email" required />
</form-matching-fields>
<form-matching-fields
validation-message="{label_1} और {label_2} का मान समान होना चाहिए।"
>
<label for="password-hi">पासवर्ड</label>
<input id="password-hi" type="password" required />
<label for="password-hi-again">पासवर्ड फिर से</label>
<input id="password-hi-again" type="password" required />
</form-matching-fields>
This component uses modern web standards:
For older browsers, you may need polyfills.
# Install dependencies
npm install
# Run tests
npm test
# Run tests with coverage
npm run test:coverage
# Lint code
npm run lint
# Format code
npm run format
# View demo
open demo/index.html
MIT © Aaron Gustafson