Log inSign up free
Getting Started, HTML
  1. Getting started

    1. Lesson 1: What Is HTML?
    2. Lesson 2: HTML Document Structure
    3. Lesson 3: HTML Tags
    4. Lesson 4: HTML Links
    5. Lesson 5: HTML Images
    6. Lesson 6: HTML Lists
    7. Lesson 7: HTML Tables
    8. Lesson 8: HTML Forms
    9. Lesson 9: Semantic HTML
Getting Started, HTML

Getting started · Lesson 8 of 9 · 4 min read · By Jack

Lesson 8: HTML Forms

By the end, you'll know how to build a form with labelled inputs, choice controls, a dropdown, a text area and a submit button, and how to use built-in validation.

Estimated time: 20 minutes (about 8 minutes reading, 12 minutes practice)

Learning goals: By the end, you'll know how to build a form with labelled inputs, choice controls, a dropdown, a text area and a submit button, and how to use built-in validation.

What is a form?
Forms collect information from users, such as sign-ups, searches and contact messages. HTML builds the form. A server or JavaScript handles the data after it's sent. We'll focus on building the form.


Part 1: The form element and text inputs (4 min)

<form action="/submit" method="post">  
  <label for="name">Your name</label>  
  <input type="text" id="name" name="name">  
  
  <button type="submit">Send</button>  
</form>
  • <form> wraps all the controls. action is where the data goes, and method is how (get or post).
  • <label> describes a control. Its for must match the input's id. Clicking the label focuses the input, and screen readers read it aloud.
  • <input> is a void element. type decides what kind of input it is.
  • name is the label attached to the data when it's sent. Without it, the value isn't submitted.

Common input types

TypeUse
textShort text
emailEmail address (checked by the browser)
passwordHidden characters
numberNumbers, with up/down arrows
dateDate picker
telPhone number
urlWeb address
searchSearch box
<label for="email">Email</label>  
<input type="email" id="email" name="email" placeholder="you@example.com">

placeholder shows a hint inside the box. It is not a replacement for a <label>.

Part 2: Choices (4 min)

Radio buttons (pick one). Give them the same name.

<fieldset>  
  <legend>Favourite language</legend>  
  <input type="radio" id="html" name="lang" value="html">  
  <label for="html">HTML</label>  
  
  <input type="radio" id="css" name="lang" value="css">  
  <label for="css">CSS</label>  
</fieldset>

Checkboxes (pick any)

<input type="checkbox" id="news" name="newsletter" value="yes">  
<label for="news">Subscribe to the newsletter</label>

Dropdown

<label for="country">Country</label>  
<select id="country" name="country">  
  <option value="">Choose one</option>  
  <option value="uk">United Kingdom</option>  
  <option value="ie">Ireland</option>  
  <option value="fr">France</option>  
</select>
  • <fieldset> groups related controls and <legend> titles the group.
  • value is what gets sent. The text between <option> tags is what the user sees.

Part 3: Text areas and buttons (3 min)

<label for="message">Message</label>  
<textarea id="message" name="message" rows="5" cols="40"></textarea>  
  
<button type="submit">Send message</button>  
<button type="reset">Clear</button>
  • <textarea> is for multi-line text and has a closing tag.
  • type="submit" sends the form. type="reset" clears it. Use type="button" for buttons that do nothing by default.

Part 4: Built-in validation (3 min)

Browsers can check input before the form is sent.

<input type="text" name="username" required minlength="3" maxlength="15">  
<input type="number" name="age" min="18" max="99">  
<input type="text" name="postcode" pattern="[A-Za-z0-9 ]{5,8}" title="5 to 8 letters or numbers">
AttributeEffect
requiredMust be filled in
minlength, maxlengthLimits text length
min, maxLimits numbers and dates
patternMust match a regular expression
disabledCan't be edited or submitted

Validation in the browser is for convenience. A real site must also check data on the server.


A complete contact form

<form action="/contact" method="post">  
  <label for="fullname">Full name</label>  
  <input type="text" id="fullname" name="fullname" required>  
  
  <label for="mail">Email</label>  
  <input type="email" id="mail" name="mail" required>  
  
  <label for="topic">Topic</label>  
  <select id="topic" name="topic">  
    <option value="general">General question</option>  
    <option value="support">Support</option>  
  </select>  
  
  <label for="msg">Message</label>  
  <textarea id="msg" name="msg" rows="5" required></textarea>  
  
  <button type="submit">Send</button>  
</form>

Common mistakes

  • Missing <label>, or a for that doesn't match the id
  • Leaving out name, so the data isn't sent
  • Giving radio buttons different name values, so more than one can be picked
  • Using placeholder instead of a label
  • Forgetting a submit button
  • Relying only on browser validation

Key takeaways

  • <form> wraps controls, and <label> connects text to each one.
  • type on <input> changes its behaviour. Pick the most specific type.
  • Radios share a name. Checkboxes allow several choices.
  • <select> and <textarea> handle dropdowns and long text.
  • Attributes like required and pattern add validation.

Exercises (12 min)

  1. Identify (1 min). Which control suits each?
    • a) Choosing one size from S, M, L
    • b) Agreeing to terms
    • c) Typing a long comment

    Answers: a) radio buttons (or <select>), b) checkbox, c) <textarea>.
  2. Fix the errors (3 min).
    <form>  
      Email: <input type="email">  
      <input type="radio" name="size" value="s"> Small  
      <input type="radio" name="sz" value="m"> Medium  
      <textarea name="note" />  
    </form>

    Hints: add a <label> with matching for and id, add name to the email input, use the same name for both radios, close <textarea></textarea>, and add a submit button.
  3. Build it (6 min). Create forms.html with a sign-up form containing:
    • a text input for name and an email input, both required
    • a password input with minlength="8"
    • a group of radio buttons inside a <fieldset> with a <legend>
    • a <select> dropdown with three options
    • a checkbox for agreeing to terms
    • a <textarea> and a submit button
  4. Challenge (2 min). Add a date input and a number input with min and max. Try submitting with bad values and see the browser's messages.

Lesson 8 checkpoint

  • I can build a form with labelled inputs.
  • I can use radios, checkboxes, <select> and <textarea>.
  • I can add validation attributes.
  • I know why name and <label> matter.

Keep track of your learning

Create a free account to unlock them and track your progress.

  • Track your progress through every course
  • Take the quizzes and see your score
  • Download the worksheets and resources
  • Get a certificate when you finish

Related reading on our blog

Rather have it done for you?

We build websites for small businesses

Everything in this course is what we do every day for local businesses: fast, mobile-friendly sites that are built to be found on Google. See our web design services or our website packages.