Unays Learn · HTML

HTML forms: inputs, labels and validation

Forms let visitors send information: a sign-up, a quiz answer, a search. This tutorial shows how to build a form that is easy to use, works with screen readers and checks input before it is sent.

The form element

All fields live inside a <form>. Its action says where the data is sent and method says how (usually post for data that changes something):

<form action="/signup" method="post">
  <!-- fields go here -->
  <button type="submit">Sign up</button>
</form>

Always use labels

Every input needs a visible <label>. Connect them by giving the input an id and the label a matching for:

<label for="name">Your name</label>
<input id="name" name="name" type="text" autocomplete="name">

Clicking the label now focuses the input, and screen readers read the label aloud. The name attribute is the key the server receives. Placeholder text is not a replacement for a label, because it disappears as soon as someone starts typing.

Choose the right input type

<label for="email">Email</label>
<input id="email" name="email" type="email" autocomplete="email">

<label for="age">Age</label>
<input id="age" name="age" type="number" min="5" max="120">

<label for="start">Start date</label>
<input id="start" name="start" type="date">

The type changes the keyboard on phones (an @ key for email, digits for numbers) and lets the browser check the value.

Checkboxes, radio buttons and menus

Group related choices in a <fieldset> with a <legend>. Radio buttons with the same name allow only one choice:

<fieldset>
  <legend>Favourite language</legend>
  <label><input type="radio" name="lang" value="python"> Python</label>
  <label><input type="radio" name="lang" value="js"> JavaScript</label>
</fieldset>

<label><input type="checkbox" name="news" value="yes"> Send me updates</label>

<label for="grade">Grade</label>
<select id="grade" name="grade">
  <option value="">Choose…</option>
  <option>6</option>
  <option>7</option>
  <option>8</option>
</select>

Wrapping an input inside its label, as above, is another valid way to connect them.

Built-in validation

HTML can check input before the form is sent:

<input id="username" name="username" required minlength="3" maxlength="20"
       pattern="[a-z0-9_]+" title="Lowercase letters, numbers and _ only">
  • required: the field cannot be empty.
  • minlength and maxlength: limits on text length.
  • min and max: limits on numbers and dates.
  • pattern: a regular expression the whole value must match.

Browser checks help users, but anyone can bypass them. A real website must always check the data again on the server.

Try it yourself

In Unays Code, build a club sign-up form with name, email, grade (a select menu), a favourite subject (radio buttons) and a required "I agree to the rules" checkbox. Try submitting it empty to see validation in action.

This tutorial was written with AI assistance and reviewed by Unays. The runnable code examples were tested before publishing. Spotted a mistake? Tell us at [email protected].