Skip to main content
⚑ OpenDevDocs
HTML
frontendhtmlGuideBeginner

HTML Forms and Input Validation

Building interactive HTML forms: input types, labels, fieldsets, accessible buttons, native validation constraints, and FormData.

Verified: 2026-09-30
Platforms:
web
#html#forms#inputs#validation#web

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

TypePurposeMobile Keyboard Triggered
type="text"General single-line textStandard text keyboard
type="email"Validates email syntaxKeyboard with @ and .com shortcuts
type="password"Masks entered charactersStandard keyboard with autofill prompt
type="number"Numeric integer/float (min, max, step)Number pad keyboard
type="tel"Telephone numbersTelephone dial pad
type="checkbox"Binary toggle (multiple selection)Checkbox box
type="radio"Single selection from a group sharing the same nameRadio 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";
  }
});

On this page