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.actionis where the data goes, andmethodis how (getorpost). -
<label>describes a control. Itsformust match the input'sid. Clicking the label focuses the input, and screen readers read it aloud. -
<input>is a void element.typedecides what kind of input it is. -
nameis the label attached to the data when it's sent. Without it, the value isn't submitted.
Common input types
| Type | Use |
|---|---|
text | Short text |
email | Email address (checked by the browser) |
password | Hidden characters |
number | Numbers, with up/down arrows |
date | Date picker |
tel | Phone number |
url | Web address |
search | Search 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. -
valueis 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. Usetype="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">| Attribute | Effect |
|---|---|
required | Must be filled in |
minlength, maxlength | Limits text length |
min, max | Limits numbers and dates |
pattern | Must match a regular expression |
disabled | Can'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 aforthat doesn't match theid -
Leaving out
name, so the data isn't sent -
Giving radio buttons different
namevalues, so more than one can be picked -
Using
placeholderinstead of a label - Forgetting a submit button
- Relying only on browser validation
Key takeaways
-
<form>wraps controls, and<label>connects text to each one. -
typeon<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
requiredandpatternadd validation.
Exercises (12 min)
-
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>. -
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 matchingforandid, addnameto the email input, use the samenamefor both radios, close<textarea></textarea>, and add a submit button. -
Build it (6 min). Create
forms.htmlwith 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
-
a text input for name and an email input, both
-
Challenge (2 min). Add a
dateinput and anumberinput withminandmax. 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
nameand<label>matter.