pattern
Native form validation attributes that reduce JavaScript. Covers pattern, min/max/step, minlength/maxlength, required, validation pseudo-classes, and form association.
Overview
HTML provides a family of attributes for client-side form validation that work without any JavaScript. The browser checks these constraints on submit and shows native error messages when values do not match.
patternvalidates against a regular expressionmin,max,stepconstrain numeric and date valuesminlength,maxlengthconstrain text lengthrequiredprevents empty submissions
These attributes complement each other. Use them together for robust validation that works even when JavaScript fails.
Applies to: <input>, <select>, <textarea>
pattern
The pattern attribute specifies a regular expression that the input value must match for the form to submit. The regex is matched against the entire value (anchored with implicit ^ and $), so you do not need to add anchors.
Always pair pattern with a title attribute that describes the expected format in plain language. The browser includes the title text in the validation error tooltip.
| Pattern | Purpose | Title text |
|---|---|---|
[0-9]{5} | US ZIP code | "Five-digit ZIP code" |
[0-9]{5}(-[0-9]{4})? | ZIP+4 | "ZIP code (12345 or 12345-6789)" |
[0-9]{3}-[0-9]{3}-[0-9]{4} | US phone | "Format: 123-456-7890" |
[a-zA-Z][a-zA-Z0-9_-]{2,15} | Username | "3-16 characters, starts with a letter" |
[A-Z]{2}[0-9]{6} | Document ID | "Two uppercase letters followed by six digits" |
#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6}) | Hex color | "Hex color (e.g., #ff0 or #ff0000)" |
Try submitting with invalid values to see native validation messages.
Pattern Tips
- The pattern is implicitly anchored:
[0-9]{5}means^[0-9]{5}$. - Patterns are only checked when the field has a value. An empty optional field with a pattern is valid. Add
requiredif the field must be filled. patternworks ontext,tel,email,url,password, andsearchinput types. It does not apply tonumber,date, orrangetypes.
min, max, step
These attributes constrain numeric, date, time, and range inputs. The browser rejects values outside the range or misaligned with the step.
| Attribute | Purpose | Applies to |
|---|---|---|
min | Minimum allowed value | number, range, date, time, datetime-local, month, week |
max | Maximum allowed value | Same as min |
step | Legal increment from the min value | Same as min |
Step Mismatch
If a value does not align with the step, the browser shows a "step mismatch" error on submit. For example, with min="0" step="5", the value 3 is invalid because it is not a multiple of 5 from the minimum. Use step="any" to accept any value within the min/max range.
minlength and maxlength
These attributes constrain the number of characters in text inputs and textareas.
| Attribute | Behavior |
|---|---|
maxlength | Prevents typing beyond the limit. The browser silently stops accepting characters. |
minlength | Validates on submit. The browser shows an error if the value is too short. |
Applies to: <input> (text types), <textarea>
Visible Character Counter
The VB data-count attribute adds a visible "X / Y" character counter when used alongside maxlength. No JavaScript setup required.
required
The required attribute prevents form submission when a field is empty. It is part of the same constraint validation system as pattern and min/max.
Applies to: <input>, <select>, <textarea>
For checkboxes, required means the checkbox must be checked. For radio buttons, at least one radio in the group must be selected. For <select>, the first option must have an empty value (value="") to serve as the placeholder.
Validation Pseudo-Classes
CSS pseudo-classes let you style fields based on their validation state. No JavaScript needed.
| Pseudo-class | Matches when |
|---|---|
:valid | The field's value satisfies all constraints |
:invalid | The field's value violates a constraint |
:user-valid | Valid, and the user has interacted with the field |
:user-invalid | Invalid, and the user has interacted with the field |
:required | The field has the required attribute |
:optional | The field does not have required |
:in-range | The value is within min/max bounds |
:out-of-range | The value is outside min/max bounds |
Prefer :user-invalid Over :invalid
The :invalid pseudo-class applies immediately on page load, which means required fields show error styles before the user has done anything. Use :user-invalid instead to only show error styles after the user has interacted with the field (typed, blurred, or submitted). This provides a much better user experience.
form and formaction
Two association attributes let you break out of normal form boundaries.
The form Attribute
The form attribute associates a control with a <form> element elsewhere on the page by referencing its id. The control does not need to be a descendant of the form.
The formaction Attribute
The formaction attribute on a submit button overrides the form's action URL for that specific submission. Similarly, formmethod, formenctype, and formnovalidate override their counterparts on the form element.
| Button attribute | Overrides |
|---|---|
formaction | The form's action URL |
formmethod | The form's method (GET/POST) |
formenctype | The form's enctype |
formnovalidate | Skips constraint validation for this submission |
Combining Constraints
Validation attributes work together. A single input can use required, pattern, minlength, and maxlength simultaneously. The browser checks all constraints and reports the first failure.
Try submitting to see how multiple constraints are validated together.
See Also
autocompletefor browser autofill hintsdata-countfor visible character countersdata-maskfor input masking<input>element reference<form>element reference<select>element reference