Reading history

HTML Forms: Inputs, Labels, Validation and Buttons

5 min read 2 views

Learn HTML forms: action and method, input types, labels, select and textarea, radio buttons and checkboxes, and built-in validation.

Forms are how visitors talk back to a website: sign-up pages, search boxes, contact forms and checkouts are all forms. HTML provides the controls (text boxes, menus, checkboxes, buttons) and can even check the input before it is sent. This lesson covers the <form> element, the most useful input types, labels, grouping, and built-in validation.

In this lesson you will learn

  • What action, method and name do
  • Text, email, number, date and password inputs
  • Menus, text areas, radio buttons and checkboxes
  • Why every control needs a <label>
  • Built-in validation with required, min, max and minlength

A Registration Form

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Form</title>
  <style>
    label { display: block; margin-top: 8px; }
    input, select, textarea { width: 220px; }
  </style>
</head>
<body>
  <form action="/register" method="post">
    <label for="name">Full name</label>
    <input type="text" id="name" name="name" required placeholder="Your name">

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

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

    <label for="course">Course</label>
    <select id="course" name="course">
      <option value="python">Python</option>
      <option value="java">Java</option>
    </select>

    <label for="msg">Message</label>
    <textarea id="msg" name="msg" rows="2"></textarea>

    <p><button type="submit">Register</button></p>
  </form>
</body>
</html>

Result

  • action is the address the data is sent to; method="post" sends it in the request body, while get puts it in the URL (fine for searches, wrong for passwords).
  • Each control’s name becomes the key in the submitted data, such as email=....
  • <label for="email"> is linked to id="email". Clicking the label focuses the box, and screen readers announce it.
  • type="email" and type="number" give the right keyboard on phones and check the format.
  • required stops the form being sent while the field is empty.

Always validate on the server too

Browser checks are for convenience. Anyone can bypass them, so the server must check every value again before using it.

Radio Buttons, Checkboxes and More

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Choices</title>
</head>
<body>
  <form>
    <fieldset>
      <legend>Preferred class time</legend>
      <label><input type="radio" name="time" value="morning" checked> Morning</label>
      <label><input type="radio" name="time" value="evening"> Evening</label>
    </fieldset>
    <fieldset>
      <legend>Topics</legend>
      <label><input type="checkbox" name="topic" value="html" checked> HTML</label>
      <label><input type="checkbox" name="topic" value="css"> CSS</label>
    </fieldset>
    <p>
      <label>Start date <input type="date" name="start"></label>
      <label>Password <input type="password" name="pw" minlength="8"></label>
    </p>
  </form>
</body>
</html>

Result

  • Radio buttons that share a name form a group where only one can be chosen.
  • Checkboxes allow any number of choices.
  • checked pre-selects an option.
  • <fieldset> and <legend> group related controls under a caption.
  • Wrapping a control inside its <label> is another way to link them.

Useful Input Types

TypeUse
text, passwordShort text; password hides characters
email, tel, urlContact details, with mobile keyboards to match
number, rangeNumbers, with min, max and step
date, timeBuilt-in pickers
checkbox, radioChoices
fileUploads (the form needs enctype="multipart/form-data")
search, color, hiddenSearch boxes, colour pickers, values the user does not see

Common Mistakes

  1. Using placeholder text instead of a label. The placeholder disappears as soon as the user types.
  2. Forgetting name, so the value is never sent.
  3. Sending passwords with method="get", which puts them in the address bar and server logs.
  4. Trusting browser validation alone.

Practice Exercises

Exercise 1. Create a search form that sends q to /search using GET.

Show answer
<form action="/search" method="get">
  <label for="q">Search</label>
  <input type="search" id="q" name="q">
  <button>Go</button>
</form>

Exercise 2. Add a phone field that requires 11 digits.

Show answer
<label for="phone">Mobile</label>
<input type="tel" id="phone" name="phone" pattern="[0-9]{11}" required>

Frequently Asked Questions

What is the difference between GET and POST?

GET adds the data to the URL, so it can be bookmarked; use it for searches and filters. POST sends the data in the request body; use it for anything that changes data or contains private information.

What happens when a form is submitted without an action?

It is sent to the current page’s URL.

Summary

  • <form> sends named values to the action URL using get or post.
  • Pick the input type that matches the data.
  • Link every control to a <label>.
  • Use required, min, max and pattern, and validate again on the server.

Next: HTML Semantic Elements.

Lessons are also marked complete when you click Next.

Leave a Reply

Your email address will not be published. Required fields are marked *