HTML Forms — Collecting User Input
Forms are how websites talk back to users — login pages, registration forms, contact pages, search bars, and checkout flows all rely on HTML forms. In this chapter you'll master every form element, all input types, client-side validation, and accessibility best practices that professional developers follow.
<form> tag — action, method, and enctype<input> types —
text, email, password, number, and more<label> tag and why
it is required for accessibility<textarea>, <select>, checkbox, radio buttons<fieldset> and <legend> for grouping fieldsWhat is an HTML Form?
An HTML form collects data from the user and sends it to a server for processing. The
<form> element is the container — it defines
where the data goes (action) and how
it is sent (method). Everything the user fills in — text fields,
dropdowns, checkboxes — lives inside this container.
<form
action="/submit-form"
method="POST"
enctype="multipart/form-data"
novalidate
>
<!-- form elements go here -->
</form>
action="/contact".GET appends data to the URL (visible,
bookmarkable — use for search). POST sends data in the request body
(hidden, secure — use for login, registration, sensitive data).multipart/form-data to allow files to be sent correctly.The <label> & <input> Tags
Every input field must have a corresponding <label>. Labels are not just visual text — they are essential for
accessibility: screen readers read the label aloud when the input is focused, and clicking the label focuses
the input automatically.
<!-- Method 1: for + id (recommended) -->
<label for="username">Username</label>
<input
type="text"
id="username"
name="username"
placeholder="Enter your username"
>
<!-- Method 2: wrap input inside label -->
<label>
Username
<input type="text" name="username">
</label>
for attribute on <label>
must match the id of the input exactly. An input without a linked label
fails accessibility audits and violates WCAG standards. Using placeholder text alone is not a substitute for
a label.
All HTML5 Input Types
The type attribute on <input> determines what kind of data the field accepts and what UI
control the browser renders. Here are all the essential types:
type is omitted. Use for names, usernames, addresses.min, max, and step attributes for range control.pattern attribute to enforce a phone number format.YYYY-MM-DD. Supports min and max to restrict selectable dates.checked attribute
pre-checks it.name can be selected at a time. Use for single-choice options.accept attribute
to restrict file types (e.g. accept=".pdf,.jpg"). Requires enctype="multipart/form-data" on the form.min, max, and step to define the scale. Great for rating or budget filters.#e44d26). Great for theme customisers.<textarea> and <select>
Two essential form elements beyond basic <input>:
<!-- Multi-line text area -->
<label for="message">Message</label>
<textarea
id="message"
name="message"
rows="5"
cols="40"
placeholder="Write your message here..."
maxlength="500"
></textarea>
<!-- Dropdown select -->
<label for="course">Choose a Course</label>
<select id="course" name="course">
<option value="">— Select —</option>
<optgroup label="Web Development">
<option value="html">HTML & CSS</option>
<option value="js">JavaScript</option>
</optgroup>
<optgroup label="Programming">
<option value="python">Python</option>
</optgroup>
</select>
<input>, <textarea> has a closing tag. Any text placed between
opening and closing tags becomes the default value. Do not use the value
attribute — it won't work on textareas.
<fieldset> and <legend>
<fieldset> groups related form fields
together with a visual box. <legend> provides a title for that
group. This is especially important for radio button groups and checkboxes — it tells screen readers what
the group is about.
<form action="/enroll" method="POST">
<fieldset>
<legend>Personal Details</legend>
<label for="fname">First Name</label>
<input type="text" id="fname" name="fname">
<label for="email">Email</label>
<input type="email" id="email" name="email">
</fieldset>
<fieldset>
<legend>Preferred Learning Mode</legend>
<label>
<input type="radio" name="mode" value="online"> Online
</label>
<label>
<input type="radio" name="mode" value="offline"> Offline
</label>
</fieldset>
<button type="submit">Enroll Now</button>
</form>
HTML5 Built-in Form Validation
HTML5 provides powerful built-in validation attributes. The browser validates the form automatically when the submit button is clicked — no JavaScript required for basic validation.
| Attribute | Applied To | Purpose | Example |
|---|---|---|---|
| required | Any input | Field must not be empty — browser blocks submission if blank | required |
| minlength | text, password, textarea | Minimum number of characters allowed | minlength="8" |
| maxlength | text, password, textarea | Maximum number of characters allowed — hard stops input at limit | maxlength="100" |
| min | number, date, range | Minimum acceptable value | min="18" |
| max | number, date, range | Maximum acceptable value | max="120" |
| pattern | text, tel, email | A regular expression the value must match — useful for phone numbers and postal codes | pattern="[0-9]{10}" |
| step | number, range, date | Legal number intervals — e.g. step="5" only allows 0, 5, 10,
15... |
step="0.5" |
| disabled | Any input | Disables the field — not submitted with the form. Greyed out visually. | disabled |
| readonly | text, textarea | User cannot edit the value but it IS submitted with the form | readonly |
| autocomplete | Any input | Controls browser autofill — on (default), off, or a specific hint like "email" |
autocomplete="email" |
<form action="/register" method="POST">
<label for="uname">Username</label>
<input
type="text"
id="uname"
name="username"
required
minlength="3"
maxlength="20"
pattern="[a-zA-Z0-9_]+"
placeholder="3–20 chars, letters & numbers only"
>
<label for="pwd">Password</label>
<input
type="password"
id="pwd"
name="password"
required
minlength="8"
autocomplete="new-password"
>
<label for="age">Age</label>
<input
type="number"
id="age"
name="age"
min="18"
max="99"
required
>
<button type="submit">Create Account</button>
</form>
Button Types — <button>
HTML provides three button types. Choosing the wrong type is a common mistake — especially inside forms.
<form>. If you omit the type attribute, the browser treats it as submit. Can unexpectedly submit
forms if placed inside one without intent.<!-- Submits the form -->
<button type="submit">Send Message</button>
<!-- Resets all fields -->
<button type="reset">Clear Form</button>
<!-- Does nothing — attach a JS listener -->
<button type="button" onclick="handleClick()">Preview</button>
Common Mistakes — Do's & Don'ts
These are the most frequent form mistakes beginners make:
<!-- Always link labels to inputs -->
<label for="email">Email</label>
<input type="email"
id="email"
name="email"
required>
<!-- Use type="button" for JS actions -->
<button type="button"
onclick="openModal()">
Preview
</button>
<!-- Input without a label (inaccessible) -->
<input type="text"
placeholder="Email">
<!-- Missing type — defaults to submit -->
<button onclick="openModal()">
Preview ← submits form!
</button>
<!-- Placeholder as only label -->
<input type="email"
placeholder="Enter email">
<label>. A placeholder is only a hint about the expected
format or example value.
Complete Example — Registration Form
Here is a full, valid, accessible registration form combining everything covered in this chapter:
<form action="/register" method="POST">
<fieldset>
<legend>Personal Information</legend>
<label for="fname">First Name</label>
<input type="text" id="fname" name="first_name"
required minlength="2" autocomplete="given-name">
<label for="email">Email Address</label>
<input type="email" id="email" name="email"
required autocomplete="email">
<label for="pwd">Password</label>
<input type="password" id="pwd" name="password"
required minlength="8" autocomplete="new-password">
</fieldset>
<fieldset>
<legend>Course Preference</legend>
<label for="course">Choose Course</label>
<select id="course" name="course" required>
<option value="">— Select —</option>
<option value="webdev">Web Development</option>
<option value="python">Python</option>
</select>
<!-- Radio group for learning mode -->
<p>Mode:</p>
<label><input type="radio" name="mode" value="online" required> Online</label>
<label><input type="radio" name="mode" value="offline"> Offline</label>
</fieldset>
<label for="msg">Message (optional)</label>
<textarea id="msg" name="message"
rows="4" maxlength="300"></textarea>
<label>
<input type="checkbox" name="terms" required>
I agree to the terms and conditions
</label>
<button type="submit">Create Account</button>
</form>
Try It Yourself
Edit the form below and click Open in Editor to test it live in full screen — including the HTML5 validation by clicking the submit button with empty fields.
pattern validation reject it. No JavaScript needed!
Quick Check — Chapter 10
Expand each question to check your understanding before moving on to the multiple choice quiz below.
GET appends the form data to the URL as query parameters — for
example /search?q=html+tutorial. The data is visible in the
browser address bar, can be bookmarked, and is stored in browser history. Use GET for search
forms and non-sensitive requests. POST sends data in the
HTTP request body, invisible in the URL. Use POST for login, registration, payment, and any
form that submits sensitive or large amounts of data. POST is also required when uploading files.
<label> is always visible, is read aloud by
screen readers when the input is focused, and clicking the label focuses the input — improving
usability. Using only placeholders fails WCAG accessibility standards and makes forms very difficult for
people with cognitive disabilities.
disabled input is excluded from form submission — its
value is never sent to the server. It also appears greyed out and cannot be focused. A readonly input is submitted with the form — its value
is sent to the server — but the user cannot modify it. Use readonly
when you want to show the user a pre-filled value that must be submitted unchanged (like a hidden order
reference), and disabled when the field is truly inactive and
irrelevant to the current form submission.
name) can be selected at a time. Selecting one deselects the others. Use
for "Choose one" scenarios like payment method or learning mode.
<button> inside a <form> is submit.
This means any button without an explicit type attribute will submit
the form when clicked — even if that was not your intent. This is a very common bug: a developer adds
<button onclick="openModal()">Preview</button> expecting
it to open a modal, but it submits the form instead. Always write type="button" for non-submit buttons, type="submit" for submit buttons, and type="reset" for reset buttons.
enctype="multipart/form-data" is only required when the form
includes a file upload (type="file"). The default
encoding (application/x-www-form-urlencoded) URL-encodes the form
data as text — it cannot handle binary file data. multipart/form-data splits the form data into multiple parts, allowing
binary files to be transmitted alongside text fields. Without it, the file will not be sent correctly to
the server. Also note: file uploads always require method="POST" —
you cannot upload files via GET.
<label> to
an <input>?type of a <button> element placed inside a <form>?- The
<form>tag defines where (action) and how (method) data is sent. UsePOSTfor sensitive data;GETfor search/filter. Addenctype="multipart/form-data"only for file uploads. - Every input must have a linked
<label>usingformatching the input'sid. Placeholder text is not a label substitute. - HTML5 provides many
typevalues —text,email,password,number,tel,date,checkbox,radio,file,range,color,hidden. Each triggers the right keyboard on mobile. <textarea>is for multi-line text (has a closing tag).<select>+<option>creates a dropdown. Use<optgroup>to group options.<fieldset>+<legend>groups related fields semantically — essential for radio button groups.- HTML5 validation:
required,minlength,maxlength,min,max,pattern— all validated by the browser with no JavaScript needed. - Always specify
typeon<button>. Default issubmit— causing accidental form submissions if omitted on non-submit buttons.
Rate Us on Google
Join Our Learning Community
Connect with thousands of learners, get daily tips, ask questions, and stay updated with new tutorials.