HTML Forms and Input Validation
Building interactive HTML forms: input types, labels, fieldsets, accessible buttons, native validation constraints, and FormData.
HTML Forms and Input Validation
Forms allow users to send data to web servers, submit search queries, authenticate credentials, and trigger backend Server Actions.
1. Accessible Form Structure
Every input element must have an associated <label> using the for attribute matching the input's id:
<form action="/api/login" method="POST">
<div class="form-group">
<label for="user-email">Email Address</label>
<input
type="email"
id="user-email"
name="email"
required
autocomplete="email"
placeholder="alex@example.com"
/>
</div>
<div class="form-group">
<label for="user-password">Password</label>
<input
type="password"
id="user-password"
name="password"
required
minlength="8"
autocomplete="current-password"
/>
</div>
<button type="submit">Log In</button>
</form>2. Common Input Types and Attributes
| Type | Purpose | Mobile Keyboard Triggered |
|---|---|---|
type="text" | General single-line text | Standard text keyboard |
type="email" | Validates email syntax | Keyboard with @ and .com shortcuts |
type="password" | Masks entered characters | Standard keyboard with autofill prompt |
type="number" | Numeric integer/float (min, max, step) | Number pad keyboard |
type="tel" | Telephone numbers | Telephone dial pad |
type="checkbox" | Binary toggle (multiple selection) | Checkbox box |
type="radio" | Single selection from a group sharing the same name | Radio circle |
type="file" | Upload files (accept="image/*", multiple) | File picker dialog |
3. Client-Side Validation Attributes
HTML5 provides built-in browser validation attributes that prevent form submission if criteria are unmet:
required: Input must not be empty.minlength/maxlength: String character count bounds.min/max: Numerical or date range limits.pattern: Regular expression validation (e.g.pattern="[0-9]{5}"for 5-digit zip codes).
4. Handling Form Submissions in JavaScript
const form = document.querySelector("form");
form.addEventListener("submit", async (event) => {
event.preventDefault(); // Stop full-page reload
const formData = new FormData(form);
const data = Object.fromEntries(formData.entries());
const response = await fetch("/api/login", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
});
if (response.ok) {
window.location.href = "/dashboard";
}
});Related Topics
Semantic HTML5 Elements
Why semantic HTML matters: <header>, <nav>, <main>, <article>, <section>, <aside>, <footer>, SEO benefits, and replacing div-soup.
Web Accessibility (a11y) Fundamentals
Core web accessibility principles: WCAG standards, ARIA attributes, keyboard navigation, focus management, color contrast, and screen reader testing.