HTML Story

HTML Story: Forms

structure, input types, labels, grouping, validation

6.0 HTML Forms

Forms are the primary mechanism for collecting user input in HTML. A <form> groups controls (inputs, selects, text areas, buttons) and describes how to submit the collected data. HTML5 added many input types and built-in constraint validation, reducing the need for JavaScript validation of common cases.

6.1 The form Element

The <form> element defines the submission target and method:
<form action="/submit" method="post" enctype="multipart/form-data">
  <!-- controls here -->
  <button type="submit">Send</button>
</form>
form attributes
AttributeEffect
actionURL where form data is sent. Defaults to the current page.
methodget appends data to the URL; post sends data in the request body.
enctypeEncoding for POST. Use multipart/form-data when uploading files.
novalidateDisables browser built-in constraint validation on submission.
autocompleteon (default) or off for the entire form.

6.2 Input Types

The <input> element takes many forms depending on its type attribute:
<input type="text"     name="username"  placeholder="Your name">
<input type="email"    name="email"     required>
<input type="password" name="pwd"       minlength="8">
<input type="number"   name="age"       min="0" max="120">
<input type="date"     name="dob">
<input type="checkbox" name="agree"     value="yes">
<input type="radio"    name="color"     value="red">
<input type="file"     name="upload"    accept=".pdf,.docx">
<input type="range"    name="volume"    min="0" max="100" step="5">
<input type="hidden"   name="token"     value="abc123">
<input type="submit"   value="Send">
<input type="reset"    value="Clear">
Input type reference
TypeControl / Notes
textSingle-line plain text.
emailText validated as an email address. Shows email keyboard on mobile.
passwordText with characters masked.
numberNumeric input with optional min/max/step.
rangeSlider for numeric range.
date / time / datetime-localDate/time pickers (appearance varies by browser).
checkboxBoolean toggle. Multiple checkboxes can share a name.
radioSingle choice from a group sharing the same name.
select + optionDropdown list (separate element — see below).
fileFile picker. Use accept to filter types; multiple for multiple files.
hiddenNot displayed. Passes data with the form silently.
searchLike text but may show a clear button; matches ::-webkit-search-cancel-button.
urlText validated as an absolute URL.
telTelephone number. Shows dial-pad keyboard on mobile; no format validation.
colorColor picker. Value is a hex string like #ff0000.
submit / reset / buttonButtons that submit the form, clear it, or do nothing by default.

6.3 Labels and Grouping

Every form control must have a visible label associated with it. The <label> element associates a text label with a control either by wrapping it or by matching for to the control's id:
<!-- Wrapping (implicit association) -->
<label>
  Username
  <input type="text" name="username">
</label>

<!-- for/id (explicit association) -->
<label for="email-field">Email address</label>
<input id="email-field" type="email" name="email">
Use <fieldset> with a <legend> to group related controls — commonly used for radio buttons or checkboxes:
<fieldset>
  <legend>Preferred contact method</legend>
  <label><input type="radio" name="contact" value="email"> Email</label>
  <label><input type="radio" name="contact" value="phone"> Phone</label>
</fieldset>

6.4 Textarea and Select

<textarea> provides a resizable multi-line text field; <select> creates a dropdown list:
<label for="bio">Bio</label>
<textarea id="bio" name="bio" rows="5" cols="40"
          maxlength="500" placeholder="Tell us about yourself..."></textarea>

<label for="lang">Preferred language</label>
<select id="lang" name="lang">
  <option value="">-- choose --</option>
  <optgroup label="Systems">
    <option value="rust">Rust</option>
    <option value="cpp">C++</option>
  </optgroup>
  <optgroup label="Web">
    <option value="js">JavaScript</option>
  </optgroup>
</select>

6.5 Constraint Validation

HTML5 built-in validation checks constraints set as attributes before the form is submitted. The browser blocks submission and highlights the first failing control:
  • required — field must not be empty.
  • minlength / maxlength — minimum / maximum character count for text inputs.
  • min / max — numeric or date bounds.
  • pattern — regular expression the value must match (anchored to the full value).
  • type="email", type="url" — format validation built into the type.
<input type="text" name="zip"
       pattern="[0-9]{5}"
       title="Five-digit ZIP code"
       required>
CSS pseudo-classes :valid, :invalid, and :user-invalid let you style controls based on their validation state without JavaScript.

6.6 References

Forms References
ResourceDescription
MDN: Web forms guide Comprehensive tutorial series from form basics through advanced patterns.
MDN: <input> element All input types and attributes documented with examples.
MDN: Constraint validation Built-in validation rules, attributes, and CSS integration.
web.dev: Learn Forms Modern forms design, accessibility, and validation patterns.
w3schools: HTML Forms Quick reference with live examples for all form elements.