Lesson 4 of 5 · 5 min

Forms

Collect input with labels, the right input types, built-in validation, grouped options and buttons, and send it to a server.

On this page
  1. A complete small form
  2. Always use labels
  3. Input types
  4. Built-in validation
  5. Choices
  6. Buttons
  7. Help text and errors
  8. Try it

Forms are how people talk back to a website: search, sign up, check out, contact. HTML does a lot of the work for you if you use the right elements.

A complete small form

<form action="/contact" method="post">
  <label for="name">Name</label>
  <input id="name" name="name" type="text" autocomplete="name" required>

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

  <label for="message">Message</label>
  <textarea id="message" name="message" rows="5" minlength="10" required></textarea>

  <button type="submit">Send</button>
</form>
  • action is where the data goes; method is how. Use post for anything that changes data, and get for searches so the query appears in the URL.
  • name is the key each value is sent under. An input without a name isn't sent.

Always use labels

A label whose for matches an input's id is read out by screen readers, and clicking the label focuses the input, which gives a bigger target on phones. A placeholder is not a label: it disappears as soon as someone starts typing.

Input types

The right type gives a better keyboard on phones and free validation:

<input type="email">     <!-- @ on the keyboard, checks the format -->
<input type="tel">       <!-- number pad -->
<input type="url">
<input type="number" min="1" max="10" step="1">
<input type="date">
<input type="password" autocomplete="current-password">
<input type="search">
<input type="checkbox">
<input type="file" accept="image/*">

autocomplete values (name, email, street-address, one-time-code and so on) let browsers and password managers fill forms correctly.

Built-in validation

<input type="text" name="username" required minlength="3" maxlength="20"
       pattern="[a-z0-9_]+" title="Lowercase letters, numbers and underscores">

The browser checks these before submitting and shows a message if something is wrong. You still must validate again on the server; anyone can bypass the browser.

Choices

Radio buttons for one choice, grouped with fieldset and legend:

<fieldset>
  <legend>Level</legend>
  <label><input type="radio" name="level" value="beginner" checked> Beginner</label>
  <label><input type="radio" name="level" value="intermediate"> Intermediate</label>
</fieldset>

Checkboxes for any number of choices:

<label><input type="checkbox" name="topics" value="css"> CSS</label>
<label><input type="checkbox" name="topics" value="sql"> SQL</label>

A dropdown for a long list:

<label for="city">City</label>
<select id="city" name="city">
  <option value="">Choose a city</option>
  <option>Bengaluru</option>
  <option>Hyderabad</option>
  <option>Pune</option>
</select>

Buttons

<button type="submit">Save</button>
<button type="button">Preview</button>  <!-- does nothing until JavaScript handles it -->
<button type="reset">Clear</button>

A <button> inside a form defaults to type="submit", so set type="button" on any button that shouldn't submit.

Help text and errors

Connect extra text to an input with aria-describedby, so screen readers read it along with the label:

<label for="password">Password</label>
<input id="password" type="password" aria-describedby="password-help">
<p id="password-help">At least 12 characters.</p>

Try it

Build a sign-up form with name, email, password, a level radio group, and a topics checkbox group. Submit it empty and see the browser's messages. Then change the form's method to get and submit it filled in: the values appear in the address bar.

More from HTML Crash Course