On this page10 sections
  1. Learning Objectives
  2. 1. A First Form
  3. 2. Input Types
  4. 3. Choices
  5. 4. Built-in Validation
  6. 5. Where the Data Goes
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 033: Forms: The Basics

Category6: Forms
Lecture033 of 048
Estimated video length16 min
TargetHTML5 · any modern browser
LevelBeginner

Sign-up pages, search boxes, contact forms: forms are how visitors send information to a site. HTML builds the form and can even check the answers before anything is sent.


Learning Objectives

  • Build a form with <form>, <label>, <input> and <button>
  • Use the right input type for text, email, password, numbers and dates
  • Add choices with radio buttons, checkboxes and <select>, and longer text with <textarea>
  • Validate input with required, minlength, min and max

1. A First Form

Example

html
<form>
  <label for="name">Your name</label>
  <input type="text" id="name" name="name">
  <button type="submit">Send</button>
</form>
  • <form> wraps all the fields.
  • <label> is the visible caption. Its for must match the input's id. Clicking the label then puts the cursor in the box, which helps on phones.
  • name is the key the value is sent under, for example name=Sara.

2. Input Types

The type attribute changes the box, and on phones it also changes the keyboard.

Example

html
<form>
  <p><label>Email <input type="email" name="email"></label></p>
  <p><label>Password <input type="password" name="password"></label></p>
  <p><label>Age <input type="number" name="age" min="1" max="120"></label></p>
  <p><label>Birthday <input type="date" name="birthday"></label></p>
  <p><label>Favourite colour <input type="color" name="colour"></label></p>
  <p><label>Volume <input type="range" name="volume" min="0" max="10"></label></p>
</form>

Wrapping the input inside the <label> (as above) links them without needing for and id.


3. Choices

Radio buttons: pick one. Give them the same name:

html
<form>
  <p>Size:</p>
  <label><input type="radio" name="size" value="s"> Small</label>
  <label><input type="radio" name="size" value="m" checked> Medium</label>
  <label><input type="radio" name="size" value="l"> Large</label>
</form>

Checkboxes: pick any number:

html
<form>
  <p>Toppings:</p>
  <label><input type="checkbox" name="extra" value="cheese"> Cheese</label>
  <label><input type="checkbox" name="extra" value="olives"> Olives</label>
</form>

Drop-down with <select>, and longer text with <textarea>:

html
<form>
  <p>
    <label for="city">City</label>
    <select id="city" name="city">
      <option>Karachi</option>
      <option>Lahore</option>
      <option selected>Islamabad</option>
    </select>
  </p>
  <p>
    <label for="msg">Message</label><br>
    <textarea id="msg" name="message" rows="4" cols="30" placeholder="Write your message…"></textarea>
  </p>
</form>

4. Built-in Validation

The browser can check answers before the form is sent. Try submitting this empty:

Example

html
<form>
  <p><label>Email (required) <input type="email" name="email" required></label></p>
  <p><label>Username (4+ letters) <input type="text" name="user" minlength="4" required></label></p>
  <p><label>Guests (1–10) <input type="number" name="guests" min="1" max="10"></label></p>
  <button type="submit">Book</button>
</form>
AttributeCheck
requiredMust not be empty
minlength / maxlengthNumber of characters
min / maxSmallest / largest number or date
type="email"Must look like an email address

Browser checks help visitors, but a real site must check again on the server, because anyone can bypass the browser.


5. Where the Data Goes

html
<form action="/subscribe" method="post">
  • action: the address the data is sent to
  • method="get": data goes in the URL (good for search forms)
  • method="post": data goes in the request body (use it for anything private, like passwords)

Common Beginner Mistakes

1. Inputs without labels. A placeholder isn't a label: it disappears when you type. Always add a <label>.

2. Missing name. An input without name isn't sent at all.

3. Radio buttons with different names. Then visitors can tick all of them. Same group, same name.

4. Trusting browser validation. Always validate on the server too.


Practice Questions

  1. Which attribute links a <label> to an input with id="phone"?
  2. Which input type shows dots instead of the typed characters?
  3. You want the visitor to choose exactly one payment method. Radio buttons or checkboxes?
  4. How do you make a field compulsory?
  5. Which element is used for a multi-line message box?
  6. Write a form with an email field (required) and a Subscribe button.

Practice Answers

  1. for="phone" on the label.
  2. type="password"
  3. Radio buttons, all with the same name.
  4. Add the required attribute.
  5. <textarea>
  6. html
    <form>
      <label for="email">Email</label>
      <input type="email" id="email" name="email" required>
      <button type="submit">Subscribe</button>
    </form>

Key Takeaways

  • <form> holds fields; every field needs a <label> and a name.
  • Pick the input type that fits the data; phones show a matching keyboard.
  • Radio = one choice (same name), checkbox = many, <select> = a drop-down, <textarea> = long text.
  • required, minlength, min, max give quick checks, but always check on the server too.