Skip to content

API Reference

Following attributes can be used on any <input /> element.

Attributes for specifying behaviour

AttributeValueDescription
validation-messageCSS selectorSpecifu where error messages should be displayed.
pattern-valueCSS selectorDynamically set a value for pattern validation from an input. Useful for stuff like password repeat.

Attributes for defining custom error messges

AttributeDescriptionCorresponding ValidityState property
message-bad-inputInput value that the browser is unable to convertbadInput
message-pattern-mismatchInput value does not match the specified patternpatternMismatch
message-range-overflowInput value is greater than the maximum specified by the max attributerangeOverflow
message-range-underflowInput value is less than the minimum specified by the min attributerangeUnderflow
message-step-mismatchInput value does not fit the rules determined by the step attribute (that is, it’s not evenly divisible by the step value)stepMismatch
message-too-longInput value exceeds the specified maxlengthtooLong
message-too-shortInput value fails to meet the specified minlengthtooShort
message-type-mismatchInput value is not in the required syntax (when type is email or url)typeMismatch
message-value-missingInput element has a required attribute, but no valuevalueMissing

Methods

In some situations you might need to tell input-validity that DOM has been changed by other library. For this purpose there is the init() method, accessible via global variable or imported from the module.

  • when input-validit is loaded directly with <script> tag:
InputValidity.init();
  • when loaded as a module:
import { init } from "input-validity";
init();

This is useful especially when HTMX updates the DOM or after Astro’s View Transition:

document.addEventListener("astro:after-swap", () => {
InputValidity.init();
});
htmx.on("htmx:load", () => {
InputValidity.init();
});