Constraint Validation API concepts
HTML constraints provide a foundation that custom interfaces can inspect and extend without discarding native validation.
✓ What you will learn in this topic
- Explain the purpose of Constraint Validation API concepts in a real HTML document.
- Choose and apply the elements shown in both examples.
- Recognize the specific mistake described in this lesson.
Concept explanation
The browser evaluates constraints such as required, type, min, max, and pattern. JavaScript can inspect validity state and validity messages, or call reportValidity to request browser feedback. Custom behavior should preserve keyboard access and communicate the error in text. Avoid suppressing native behavior unless the replacement is complete. A valid browser result means only that those client-side rules passed, not that the server should trust the request.
Syntax
Use native constraints first; enhance with validity state only when the custom feedback remains accessible.
Example 1: a small page
The constraints expose a range and required state to the browserâs validation model.
<label for=\"age\">Age</label>
<input id=\"age\" name=\"age\" type=\"number\" min=\"16\" max=\"120\" required>Expected result: Submitting without a value or outside the range triggers browser validation feedback.
Example 2: a practical page
The field type and visible hint support valid URL entry; custom JavaScript can inspect validity without changing the need for server checks.
<form>
<label for=\"website\">Portfolio URL</label>
<input id=\"website\" name=\"website\" type=\"url\" required aria-describedby=\"website-help\">
<p id=\"website-help\">Include https:// at the beginning.</p>
<button type=\"submit\">Continue</button>
</form>Expected result: The browser can prompt for a URL-shaped value and the person has a clear format hint.
Common mistakes
Do not present client validation as proof that submitted information is valid or safe.
Best practices
Use browser constraints for immediate feedback, provide clear recovery instructions, and validate all untrusted values on the server.
Practice exercise
Design a multi-step application form that uses native constraints, custom error messaging, server revalidation, and a useful correction summary.
Lesson summary
- Build on native constraints and make any custom validation feedback clear and accessible.
- Use browser constraints for immediate feedback, provide clear recovery instructions, and validate all untrusted values on the server.
Knowledge check
What does the Constraint Validation API expose to client code?
Show the explanation
It exposes whether controls satisfy their constraints and which validity conditions currently fail.
Practical example
html<label for=\"age\">Age</label>
<input id=\"age\" name=\"age\" type=\"number\" min=\"16\" max=\"120\" required>
Expected result:
Submitting without a value or outside the range triggers browser validation feedback.
Hands-on practice task
Design a multi-step application form that uses native constraints, custom error messaging, server revalidation, and a useful correction summary.
In this section
Other lessons in Advanced Forms and Validation
Broaden your knowledge