HTML Forms
Build HTML forms with input types, validation, and submission to collect user data.
What you'll learn
- How to structure an HTML form with the <form> element
- Common input types: text, email, password, number, checkbox, radio
- Built-in HTML validation with required, min, max, and pattern
- How the <label> element connects labels to inputs for accessibility
Concept
HTML Forms
Forms are how web pages collect input from users. A login form, a search box, a contact page, a shopping cart checkout — all are forms. HTML provides a rich set of form elements and input types so you can build these without any JavaScript, though JS enhances them later.
The Form Element
Every form starts with a <form> element. Two attributes matter most: action (the URL the form submits to) and method (GET or POST). GET appends data to the URL; POST sends it in the request body. Use POST for anything that changes data or contains sensitive information like passwords.
Input Types
HTML offers many <input> types, each with built-in behavior and validation:
text— single-line text.email— validates the format of an email address.password— masks characters as you type.number— numeric input with up/down steppers.checkbox— a toggle (on/off).radio— one choice from a group (all radios share the samename).<textarea>— multi-line text (not an<input>, its own element).<select>— a dropdown menu.
Labels and Accessibility
Every input should have a <label> connected via the for attribute matching the input's id. This is not optional — it is essential for accessibility. Screen readers announce the label when the input gets focus, and clicking the label focuses the input. A form without labels is unusable for keyboard and screen reader users.
Built-in Validation
HTML can validate input without JavaScript. Add required to force a field, min and max for numeric ranges, maxlength for text limits, and pattern for a regex the value must match. The browser blocks submission and shows an error bubble if validation fails. This covers most common cases — reach for JavaScript validation only when you need custom rules or cross-field checks.
Example
<form action="/api/register" method="POST">
<div>
<label for="name">Full Name</label>
<input type="text" id="name" name="name" required />
</div>
<div>
<label for="email">Email</label>
<input type="email" id="email" name="email" required />
</div>
<div>
<label for="age">Age</label>
<input type="number" id="age" name="age" min="13" max="120" />
</div>
<div>
<label for="bio">Bio (max 200 chars)</label>
<textarea id="bio" name="bio" maxlength="200"></textarea>
</div>
<button type="submit">Register</button>
</form>
Each input has a connected label via for/id. The email input validates format automatically. Age is constrained to 13–120. The textarea limits length. The required attribute prevents submission of empty name and email fields.
Try it
- HTML ↔ Markdown
See how form HTML converts to and from Markdown structures.
- JSON Formatter
Form submissions often produce JSON — format the result to inspect it.
Common mistakes
The mistake
Forgetting to connect <label> to <input> with for and id.
The fix
Always set <label for="x"> and <input id="x">. Without this, screen readers cannot announce the field, and clicking the label will not focus the input.
The mistake
Using GET for a login or registration form.
The fix
GET puts form data in the URL, exposing passwords in browser history and server logs. Always use method="POST" for sensitive data, and serve the form over HTTPS.
Related Resources
Related Snippets
- HTML Form Snippet
A ready-to-use form with all common input types and validation.
Related Cheatsheets
- HTML Cheatsheet
Reference for all form elements and input types.
Practice HTML Forms
4 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.