Lesson 9 of 23

In progress

Course progress

HTML and CSS

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.

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.

HTML
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, maxlength for length.
  • min, max, step for numbers and dates.
  • pattern for a precise format, using a regular expression.
HTML
Editable. Runs in your browser.





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.

HTML
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
HTML
Editable. Runs in your browser.





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 type on every button inside a form.
  • Use required, pattern, min and maxlength to match the data, and always explain the rule in text.
  • Connect errors with aria-describedby, mark them with aria-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?
Better to use `type=”date”` so the browser provides a picker and a consistent value format. Patterns are for text formats that have no dedicated input type.
Should the submit button say Submit?
Say what happens: “Create account”, “Send message”, “Book seats”. “Submit” describes the mechanism, not the result.

Full syllabus

Progress is saved in this browser only. No account, no email.

Admin

About the author

Admin

Instructor and technical writer at tutorials.in

Writes beginner lessons for tutorials.in. Each lesson is checked against the language or tool documentation, and examples are run before publishing.

Reviewed by the editorial team before publishing. Last updated November 16, 2026.