Forms are how visitors talk back to a website: sign-up pages, search boxes, contact forms and checkouts are all forms. HTML provides the controls (text boxes, menus, checkboxes, buttons) and can even check the input before it is sent. This lesson covers the <form> element, the most useful input types, labels, grouping, and built-in validation.
In this lesson you will learn
- What
action,methodandnamedo - Text, email, number, date and password inputs
- Menus, text areas, radio buttons and checkboxes
- Why every control needs a
<label> - Built-in validation with
required,min,maxandminlength
A Registration Form
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Form</title>
<style>
label { display: block; margin-top: 8px; }
input, select, textarea { width: 220px; }
</style>
</head>
<body>
<form action="/register" method="post">
<label for="name">Full name</label>
<input type="text" id="name" name="name" required placeholder="Your name">
<label for="email">Email</label>
<input type="email" id="email" name="email" required>
<label for="age">Age</label>
<input type="number" id="age" name="age" min="10" max="100">
<label for="course">Course</label>
<select id="course" name="course">
<option value="python">Python</option>
<option value="java">Java</option>
</select>
<label for="msg">Message</label>
<textarea id="msg" name="msg" rows="2"></textarea>
<p><button type="submit">Register</button></p>
</form>
</body>
</html>
Result
actionis the address the data is sent to;method="post"sends it in the request body, whilegetputs it in the URL (fine for searches, wrong for passwords).- Each control’s
namebecomes the key in the submitted data, such asemail=.... <label for="email">is linked toid="email". Clicking the label focuses the box, and screen readers announce it.type="email"andtype="number"give the right keyboard on phones and check the format.requiredstops the form being sent while the field is empty.
Always validate on the server too
Browser checks are for convenience. Anyone can bypass them, so the server must check every value again before using it.
Radio Buttons, Checkboxes and More
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Choices</title>
</head>
<body>
<form>
<fieldset>
<legend>Preferred class time</legend>
<label><input type="radio" name="time" value="morning" checked> Morning</label>
<label><input type="radio" name="time" value="evening"> Evening</label>
</fieldset>
<fieldset>
<legend>Topics</legend>
<label><input type="checkbox" name="topic" value="html" checked> HTML</label>
<label><input type="checkbox" name="topic" value="css"> CSS</label>
</fieldset>
<p>
<label>Start date <input type="date" name="start"></label>
<label>Password <input type="password" name="pw" minlength="8"></label>
</p>
</form>
</body>
</html>
Result
- Radio buttons that share a
nameform a group where only one can be chosen. - Checkboxes allow any number of choices.
checkedpre-selects an option.<fieldset>and<legend>group related controls under a caption.- Wrapping a control inside its
<label>is another way to link them.
Useful Input Types
| Type | Use |
|---|---|
text, password | Short text; password hides characters |
email, tel, url | Contact details, with mobile keyboards to match |
number, range | Numbers, with min, max and step |
date, time | Built-in pickers |
checkbox, radio | Choices |
file | Uploads (the form needs enctype="multipart/form-data") |
search, color, hidden | Search boxes, colour pickers, values the user does not see |
Common Mistakes
- Using placeholder text instead of a label. The placeholder disappears as soon as the user types.
- Forgetting
name, so the value is never sent. - Sending passwords with
method="get", which puts them in the address bar and server logs. - Trusting browser validation alone.
Practice Exercises
Exercise 1. Create a search form that sends q to /search using GET.
Show answer
<form action="/search" method="get">
<label for="q">Search</label>
<input type="search" id="q" name="q">
<button>Go</button>
</form>
Exercise 2. Add a phone field that requires 11 digits.
Show answer
<label for="phone">Mobile</label>
<input type="tel" id="phone" name="phone" pattern="[0-9]{11}" required>
Frequently Asked Questions
What is the difference between GET and POST?
GET adds the data to the URL, so it can be bookmarked; use it for searches and filters. POST sends the data in the request body; use it for anything that changes data or contains private information.
What happens when a form is submitted without an action?
It is sent to the current page’s URL.
Summary
<form>sends named values to theactionURL usinggetorpost.- Pick the input type that matches the data.
- Link every control to a
<label>. - Use
required,min,maxandpattern, and validate again on the server.
Next: HTML Semantic Elements.
