HTML Form Validation Attributes

required, pattern, min, max, step and the Constraint Validation API in one place.

Reference for HTML5 form validation attributes — required, pattern, min, max, minlength, step — plus the Constraint Validation API methods and ValidityState flags, with a live pattern tester.

How does the pattern attribute work?

pattern holds a JavaScript regular expression that the entire value must match — it is implicitly anchored, so the regex must match the whole string. It applies to text, search, url, tel, email and password inputs and sets the patternMismatch flag when it fails.

Native validation without JavaScript

HTML5 form validation lets the browser enforce rules declaratively through attributes, exposing the result via the Constraint Validation API. This reference covers every validation attribute, the ValidityState flag it sets, and the API methods — plus a live tester for the pattern attribute.

How it works

Validation attributes constrain a control; on submit (or via a script call) the browser checks each constraint and sets the matching ValidityState flag:

<input type="text" required minlength="3" maxlength="20"
       pattern="[a-z0-9_]+" title="lowercase, digits, underscore">
const input = form.elements.username;
if (!input.checkValidity()) {
  console.log(input.validity); // { patternMismatch: true, ... }
}
input.setCustomValidity(taken ? "Username is taken" : "");
form.reportValidity(); // shows the bubble on the first invalid field

The pattern regex is implicitly anchored to the whole value (^(?:...)$), so it must match the entire string, not a substring.

Tips and notes

  • pattern only validates non-empty values — combine with required to forbid blanks.
  • Always set a title alongside pattern; browsers show it in the error bubble.
  • min/max/step apply to numeric and date types; step mismatches set stepMismatch (e.g. step="5" rejects 7).
  • novalidate on the form or formnovalidate on a submit button skips all checks.
  • Style states with the :valid, :invalid, :required and :user-invalid CSS pseudo-classes.