Input constraints and autocomplete
Browser input constraints can prevent common mistakes before a form is sent, while autocomplete can reduce repetitive typing.
✓ What you will learn in this topic
- Explain the purpose of Input constraints and autocomplete in a real HTML document.
- Choose and apply the elements shown in both examples.
- Recognize the specific mistake described in this lesson.
Concept explanation
Use type and constraints that match the expected value: min and max for ranges, step for increments, minlength and maxlength for text length, and pattern for a carefully explained format. Required marks a field as needed. Autocomplete tokens describe familiar data such as email or postal-code. These features help users but do not validate business rules or establish trust; the server must check submitted values independently.
Syntax
Combine the suitable input type with required, min, max, step, pattern, and autocomplete only when each matches the field.
Example 1: a small page
The browser can reject values outside the available seat range.
<label for=\"seats\">Number of seats (1â6)</label>
<input id=\"seats\" name=\"seats\" type=\"number\" min=\"1\" max=\"6\" required>Expected result: A numeric control requests a required value from 1 through 6.
Example 2: a practical page
The format is constrained and described in visible text connected to the field.
<label for=\"postal\">Postal code</label>
<input id=\"postal\" name=\"postal_code\" autocomplete=\"postal-code\" pattern=\"[A-Za-z0-9 ]{3,10}\" aria-describedby=\"postal-help\" required>
<p id=\"postal-help\">Enter the code used by your delivery address.</p>Expected result: The browser checks that a value is present and within the stated pattern; the server still needs its own validation.
Common mistakes
Client-side constraints improve usability but are not a security boundary or replacement for server validation.
Best practices
Use fieldset and legend for related controls, autocomplete tokens where appropriate, and connect error text to the control.
Practice exercise
Build an accessible event registration form with grouped ticket choices, an email field, an age range, and visible guidance for invalid values.
Lesson summary
- Native constraints and autocomplete improve input, but server validation remains authoritative.
- Use fieldset and legend for related controls, autocomplete tokens where appropriate, and connect error text to the control.
Knowledge check
What does the required attribute do?
Show the explanation
It asks the browser to prevent ordinary submission while that control has no value.
Practical example
html<label for=\"seats\">Number of seats (1–6)</label>
<input id=\"seats\" name=\"seats\" type=\"number\" min=\"1\" max=\"6\" required>
Expected result:
A numeric control requests a required value from 1 through 6.
Hands-on practice task
Build an accessible event registration form with grouped ticket choices, an email field, an age range, and visible guidance for invalid values.
In this section
Other lessons in Advanced Forms
Broaden your knowledge