Forms, Input Types & Constraint Validation

Forms, Input Types & Constraint Validation in HTML: HTML Forms ( ) are the primary vehicle for capturing user input and transacting data across the web.

HTML Forms (<form>) are the primary vehicle for capturing user input and transacting data across the web. Modern HTML5 provides powerful native controls, mobile keyboard optimizations, and built-in constraint validation algorithms.


Form Element Architecture

<form 
  action="/api/register" 
  method="post" 
  enctype="application/x-www-form-urlencoded"
  autocomplete="on"
>
  • action: The target URL endpoint that receives the submission payload.
  • method:
  • GET: Appends form fields to the URL query string (/search?q=typescript). Used for non-destructive searches. Never use for passwords or personal data!
  • POST: Sends data inside the HTTP request body. Used for mutations, authentication, and state changes.
  • enctype:
  • application/x-www-form-urlencoded (Default): Key-value pairs URL-encoded.
  • multipart/form-data: Mandatory whenever uploading files via <input type="file">!

Semantic Form Controls & Label Pairing

Every form input MUST have an associated <label>. Labels improve usability: clicking a label expands the hit target by focusing the associated input, and screen readers read the label aloud upon focus:

<!-- Method A: Explicit 'for' attribute matching input 'id' (Recommended) -->
<label for="account-username">Desired Username</label>
<input type="text" id="account-username" name="username" required>

<!-- Grouping related inputs with fieldset and legend -->
<fieldset>
  <legend>Preferred Notification Channel</legend>
  
  <label>
    <input type="radio" name="notify" value="email" checked>
    Email Notifications
  </label>
  
  <label>
    <input type="radio" name="notify" value="sms">
    SMS Text Alerts
  </label>
</fieldset>

Mobile Optimization: inputmode and autocomplete

On smartphones and tablets, the browser displays different virtual software keyboards based on input configuration:

Input ConfigurationVirtual Keyboard Shown
inputmode="numeric"Number pad (0-9)
inputmode="decimal"Number pad with decimal point
inputmode="tel"Phone keypad with * and #
inputmode="email"Keyboard with @ and . readily accessible
inputmode="url"Keyboard with / and .com shortcuts
<!-- Instant 2FA Autofill from SMS on iOS and Android -->
<label for="otp">Security Code</label>
<input 
  type="text" 
  id="otp" 
  name="otp" 
  inputmode="numeric" 
  pattern="[0-9]{6}" 
  autocomplete="one-time-code" 
  required
>

Native HTML5 Constraint Validation API

Before JavaScript even runs, browsers can validate user input natively:

<input 
  type="text" 
  name="postal_code" 
  pattern="[0-9]{5}(-[0-9]{4})?" 
  title="Enter a valid 5-digit US ZIP code (e.g., 90210)"
  minlength="5"
  maxlength="10"
  required
>
  • required: Blocks submission if empty.
  • pattern="regex": Enforces a regular expression match.
  • min / max: Sets numeric or date boundaries.
  • step: Controls valid intervals (e.g., step="0.01" for currency).

Advanced Inputs: <datalist> and <textarea>

<!-- Datalist: Provides autocomplete suggestions while allowing custom text entry -->
<label for="timezone">Select Primary Timezone</label>
<input list="tz-options" id="timezone" name="timezone">

<datalist id="tz-options">
  <option value="America/New_York">
  <option value="Europe/London">
  <option value="Asia/Tokyo">
  <option value="Australia/Sydney">
</datalist>

Exercises

  1. Build a multi-step checkout form with billing address, credit card input, and native validation constraints.
  2. Implement a 6-digit SMS verification input utilizing autocomplete="one-time-code" and inputmode="numeric".
  3. Use the JavaScript Constraint Validation API (element.setCustomValidity()) to display custom translated error messages.