On this page10 sections
Lecture 033: Forms: The Basics
| Category | 6: Forms |
| Lecture | 033 of 048 |
| Estimated video length | 16 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
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
typefor text, email, password, numbers and dates - Add choices with radio buttons, checkboxes and
<select>, and longer text with<textarea> - Validate input with
required,minlength,minandmax
1. A First Form
Example
<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. Itsformust match the input'sid. Clicking the label then puts the cursor in the box, which helps on phones.nameis the key the value is sent under, for examplename=Sara.
2. Input Types
The type attribute changes the box, and on phones it also changes the keyboard.
Example
<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:
<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:
<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>:
<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
<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>| Attribute | Check |
|---|---|
required | Must not be empty |
minlength / maxlength | Number of characters |
min / max | Smallest / 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
<form action="/subscribe" method="post">action: the address the data is sent tomethod="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
- Which attribute links a
<label>to an input withid="phone"? - Which input type shows dots instead of the typed characters?
- You want the visitor to choose exactly one payment method. Radio buttons or checkboxes?
- How do you make a field compulsory?
- Which element is used for a multi-line message box?
- Write a form with an email field (required) and a Subscribe button.
Practice Answers
for="phone"on the label.type="password"- Radio buttons, all with the same
name. - Add the
requiredattribute. <textarea>-
<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 aname.- Pick the input
typethat 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,maxgive quick checks, but always check on the server too.