Lesson 9 of 23
In progress
Buttons, actions and validation states
Use buttons for actions and links for navigation, validate input with the right attributes, and show errors that help instead of blame.
A link that looks like a button and a button that navigates are both lies the browser tells assistive technology. The element you choose decides what happens on Enter, what a screen reader announces and what the browser offers.
What you will learn
- Choose a button for actions and an anchor for navigation.
- Use validation attributes that match the data.
- Present errors so they are announced and easy to fix.
Button or link?
If activating the control goes somewhere, use an a with href. If it does something on this page — opens a dialog, adds an item, submits a form — use a button.
Editable. Runs in your browser.
Press Run to see the output.
A div with a click handler is neither: it is not focusable, does not respond to Enter or Space, and has no role. If you must use a non-button element, you have to recreate all of that, and it is rarely worth it.
Always set the button type. Inside a form, the default is submit, so a "Show password" button without type="button" submits the form and loses the user’s place.
Validation attributes that fit
required handles presence. The rest depend on the data:
type="email",type="url"for shape.minlength,maxlengthfor length.min,max,stepfor numbers and dates.patternfor a precise format, using a regular expression.
Editable. Runs in your browser.
<label for="seats">Seats</label>
<input id="seats" name="seats" type="number" min="1" max="6" value="2">
<button type="submit">Book</button>
</form>
Press Run to see the output.
A pattern only checks the shape of what was typed. It cannot know whether the code exists, whether the date is available, or whether the person is allowed to book. That is server work.
Keep the hint visible and tie it to the field with aria-describedby. A pattern with no explanation is a guessing game, and the browser’s default message rarely repeats your rule.
Errors with a place and a voice
Browser validation bubbles disappear on the next interaction and are announced inconsistently. For anything important, render your own message next to the field and connect it.
Editable. Runs in your browser.
Press Run to see the output.
The message names the field, says what is wrong and shows an example. It does not say "invalid input" or "you made an error". role="alert" makes it announced when it appears; aria-invalid marks the field’s state for assistive technology. Colour alone is not enough, because not everyone sees the red border.
Put a summary at the top of a long form when submission fails, with links to each problem field. For a three-field form, inline messages are enough.
Disabled controls are a trap
A disabled button cannot be focused, so a keyboard user may never discover why they cannot continue. Prefer leaving the button enabled and explaining the problem when pressed, or use aria-disabled="true" and handle it in code.
If you must disable, put the reason in text near the button. "Complete the address to continue" is actionable; a grey rectangle is not.
Adding novalidate to a form to stop the browser’s bubbles also removes its checks entirely. Replace it with your own validation that runs on submit and on blur, marks fields with aria-invalid, and moves focus to the first error. Otherwise the form submits bad data and the server has to guess.
Build a form with a required email and a student code that must match two letters and four digits. Add visible hints for both, and an error paragraph tied to the code field that explains the format.
Show solution
Editable. Runs in your browser.
<label for="code">Student code</label>
<input id="code" name="code" type="text" required
pattern="[A-Z]{2}[0-9]{4}"
aria-describedby="code-hint code-error"
aria-invalid="true">
<p id="code-hint">Two capital letters then four digits, like CS2026.</p>
<p id="code-error" role="alert">That code does not match the format.</p>
<button type="submit">Register</button>
</form>
Press Run to see the output.
Both fields have persistent hints, and the code field’s error is connected by id so it is announced with the field. The example in the hint is what makes the pattern understandable.
Recap
- Buttons perform actions; links navigate. Set
typeon every button inside a form. - Use
required,pattern,minandmaxlengthto match the data, and always explain the rule in text. - Connect errors with
aria-describedby, mark them witharia-invalid, and avoid disabled controls without an explanation.
Next, you will cover the accessibility basics that apply to the whole page, not just forms.
Can I use pattern for dates?
Should the submit button say Submit?
Progress is saved in this browser only. No account, no email.