Build a registration form with built-in validation

Task

Build a sign-up form for an online course. The browser itself must stop the user from sending empty or malformed data — no JavaScript, only HTML attributes.

Requirements

  • Related fields are grouped in <fieldset> elements, each with a <legend>
  • Every field has its own <label for="..."> that matches the field's id
  • Email uses type="email", phone uses type="tel", password uses type="password"
  • Name, email and password are required; the password must be at least 8 characters long (minlength)
  • The phone field has a pattern for an international number and a title that explains the format
  • The course is a required <select> whose first <option> is an empty placeholder (value="")
  • The study format is a group of radio buttons with the same name, inside its own <fieldset>
  • A required checkbox to accept the terms and a <button type="submit">; no JavaScript

Starter code

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Course sign-up</title>
</head>
<body>
  <h1>Sign up for the course</h1>
  <form action="/signup" method="post">

  </form>
</body>
</html>
▶ Open in HTML Editor

Hints

  • Radio buttons form one group only when they share the same name. Putting required on one of them makes the whole group required.
  • A placeholder is not a label: it disappears as soon as the user starts typing, so every field still needs a <label>.
  • pattern must match the whole value, and the browser shows the text from the title attribute when the value doesn't match.
  • The site's preview is sandboxed, so the form can't be submitted there and validation messages won't appear. Download the file and open it in your browser to try submitting it.
Show solution
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Course sign-up</title>
</head>
<body>
  <h1>Sign up for the course</h1>
  <form action="/signup" method="post">
    <fieldset>
      <legend>Account</legend>

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

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

      <label for="phone">Phone</label>
      <input type="tel" id="phone" name="phone" autocomplete="tel"
             pattern="\+[0-9]{10,15}" title="International format, for example +380501234567">

      <label for="password">Password</label>
      <input type="password" id="password" name="password" autocomplete="new-password"
             minlength="8" required>
    </fieldset>

    <fieldset>
      <legend>Course</legend>

      <label for="course">Course</label>
      <select id="course" name="course" required>
        <option value="">— choose a course —</option>
        <option value="html">HTML basics</option>
        <option value="css">CSS layout</option>
        <option value="js">JavaScript for beginners</option>
      </select>

      <fieldset>
        <legend>Study format</legend>
        <input type="radio" id="format-online" name="format" value="online" required>
        <label for="format-online">Online</label>
        <input type="radio" id="format-class" name="format" value="class">
        <label for="format-class">In class</label>
      </fieldset>
    </fieldset>

    <input type="checkbox" id="terms" name="terms" required>
    <label for="terms">I agree to the terms of study</label>

    <button type="submit">Sign up</button>
  </form>
</body>
</html>

Related tools