Imagine asking a friend for some information on a webpage. The field should explain exactly what you need.
What You Need to Know
A validation response should identify the actual field and guide users toward it.
Try the Example
Take a look at the example. We’ll explain the important lines right below it.
<div id="form-status" role="alert"></div>
<form id="quiz-form" action="/quiz" method="post">
<label for="answer">Answer</label>
<input id="answer" name="answer" required>
<button type="submit">Check</button>
</form>
<script>
const quizForm = document.querySelector("#quiz-form");
quizForm.addEventListener("submit", (event) => {
if (!quizForm.checkValidity()) return;
document.querySelector("#form-status").textContent = "Form is ready";
});
</script>
Let’s Read the Code
- <form>: The form groups controls and identifies where submitted data goes.
- <label>: The label tells people what the input is for. A matching for/id pair makes the label clickable.
- id: An id uniquely identifies an element on this page.
- for: For connects a label or output with a control by ID.
Read the named elements and attributes together. They explain how the browser connects this feature to the rest of the page.
What Happens in the Browser?
The HTML controls work in the browser, but saving an answer requires the example action path to be handled by a real server.
Where does the answer go? The example uses the submitted name(s) answer. Those names help the receiving server identify each answer. A label is for people; a name attribute is for the form data; an ID connects elements in the page.
Your Turn
Leave the answer empty and see which browser control gets focus when submitting.
Common Beginner Mistakes
- Copying the example without giving the fields a clear label and a usable name.
- Do not move focus into a hidden or inaccessible element. Native validation may take precedence.
Key Takeaways
- A control needs a clear label and, when submitted, a meaningful name and value.
- <form>: The form groups controls and identifies where submitted data goes.
- A meaningful HTML element is usually a stronger starting point than adding an ARIA role to a generic div. Also check what happens using only the keyboard.