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:
| Attribute | Enforces |
|---|---|
required | The field must be filled |
minlength / maxlength | Text length bounds |
min / max | Number/date bounds |
pattern | A regex the value must match |
type="email" | A valid email format |
<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 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:
<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
<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
- 01Validation attributes (required, minlength/maxlength, min/max, pattern) catch errors before submit.
- 02Client validation is UX, not security — always re-validate on the server.
- 03Every input needs a <label for> (or a wrapping label) for clicks and screen readers.
- 04Group related controls in <fieldset> with a <legend> for an accessible group name.
- 05autocomplete, autofocus and <datalist> are small touches that improve real usability.
