Skip to main content
Albero Academy

Forms — Validation & UX

Catch mistakes before submit with built-in validation attributes, and build accessible, user-friendly forms with labels and fieldsets.

Chapter 9HTML14 min readBeginner friendly
  • #HTML
  • #Forms
  • #Validation

A good form catches errors early, is usable by everyone, and never surprises. HTML gives you validation and accessibility features for free — this chapter is how to use them well, so most forms need very little JavaScript.

Built-in validation

Validation attributes stop invalid submissions and show native messages — no script required:

AttributeEnforces
requiredThe field must be filled
minlength / maxlengthText length bounds
min / maxNumber/date bounds
patternA regex the value must match
type="email"A valid email format
validated fields
<input type="email" required>
<input type="text" minlength="2" maxlength="50" required>
<input type="tel" pattern="[0-9]{10}" title="10-digit number">
<input type="number" min="16" max="99" required>

Labels & accessibility

Every input needs a <label>, linked by matching for to the input's id. This makes the label clickable and lets screen readers announce the field:

label ↔ input
<label for="email">Email address</label>
<input type="email" id="email" name="email" required>
<!-- Or wrap the input in the label (no id needed) -->
<label>Email <input type="email" name="email"></label>

Fieldset & legend

Group related controls (especially radio sets) so the group has an accessible name:

grouping
<fieldset>
<legend>Preferred format</legend>
<label><input type="radio" name="fmt" value="live"> Live</label>
<label><input type="radio" name="fmt" value="recorded"> Recorded</label>
</fieldset>

UX helpers

small touches that help
<input name="name" autocomplete="name"> <!-- browser autofill -->
<input type="email" autocomplete="email" autofocus> <!-- focus on load -->
<input list="tools" name="tool">
<datalist id="tools"> <!-- suggestions -->
<option value="SQL"><option value="Python">
</datalist>

Practice

Rebuild your "Book a call" form with full validation (required, a pattern for the phone, length limits), every field labelled, the radio options in a fieldset, sensible autocomplete values, and a datalist of programme suggestions. Try submitting it empty to see the native messages.

Key takeaways

  1. 01Validation attributes (required, minlength/maxlength, min/max, pattern) catch errors before submit.
  2. 02Client validation is UX, not security — always re-validate on the server.
  3. 03Every input needs a <label for> (or a wrapping label) for clicks and screen readers.
  4. 04Group related controls in <fieldset> with a <legend> for an accessible group name.
  5. 05autocomplete, autofocus and <datalist> are small touches that improve real usability.