Skip to content

Building Forms

Forms are how users submit data to your website. From login pages to search boxes to complex surveys, forms are everywhere on the web. This tutorial covers form elements, validation, and best practices.

What you’ll learn:

  • The <form> element and attributes
  • Input types and the <input> element
  • Other form elements: <textarea>, <select>, <button>
  • Labels and accessibility
  • Form validation and required attributes
  • File uploads and date pickers
  • Best practices for user experience

Prerequisites:


Every form starts with the <form> element:

Result

The <form> element has two critical attributes:

  • action: Where the form data is sent
  • method: How the data is sent (usually POST or GET)

The <input> element is the most common form element. Different types handle different data:

Result
Result

Use type="number" for numeric input with validation. type="range" creates a slider.

Result
  • Checkboxes: Multiple selections allowed
  • Radio buttons: Only one selection allowed
  • <fieldset> and <legend>: Group related inputs and label the group
Result

These show calendar and time pickers on most devices. Users appreciate these instead of typing dates.

Result

Use accept to limit file types. Common values:

  • "image/*" — Any image format
  • ".pdf" — PDF files only
  • ".jpg,.png" — Specific formats
Result

Each type provides appropriate input methods and validation.


For longer text like comments or messages:

Result

The rows and cols attributes set the initial size (though users can resize).


For choosing from predefined options:

Result
  • Add multiple attribute to allow selecting multiple options
  • The first <option> with empty value is often a placeholder
  • Use <optgroup> to organize options:
<select name="language">
<optgroup label="Romance Languages">
<option value="es">Spanish</option>
<option value="fr">French</option>
</optgroup>
<optgroup label="Germanic Languages">
<option value="de">German</option>
<option value="en">English</option>
</optgroup>
</select>

Always connect labels to inputs with the for attribute:

Result

Connected labels:

  • Make forms easier to use (larger click area)
  • Help screen readers announce the label
  • Are required for accessibility

HTML5 provides built-in validation. Mark fields as required and specify patterns:

Result

Validation attributes:

  • required: Field must be filled
  • minlength/maxlength: String length limits
  • min/max: Number or date limits
  • pattern: Regular expression pattern
  • type="email": Validates email format

Forms need buttons for submission and other actions:

Result
  • type="submit" — Submits the form
  • type="reset" — Clears all form fields
  • type="button" — Regular button for JavaScript

Here’s a realistic contact form:

Result

Every input needs a label connected with for:

<!-- Good -->
<label for="username">Username:</label>
<input id="username" name="username">
<!-- Bad -->
<input name="username" placeholder="Username">

Use <fieldset> and <legend> to group related fields:

<fieldset>
<legend>Contact Methods</legend>
<label>
<input type="checkbox" name="contact" value="email"> Email
</label>
<label>
<input type="checkbox" name="contact" value="phone"> Phone
</label>
</fieldset>

Help users fix problems:

<label for="password">Password (minimum 8 characters):</label>
<input type="password" id="password" name="password" minlength="8" required>

Create a form for a specific purpose:

Result


  • Use <form> with proper action and method attributes
  • Choose appropriate <input> types: text, email, number, date, file, etc.
  • Use <textarea> for longer text
  • Use <select> for dropdown choices
  • Use <button> for submissions and actions
  • Always use <label> elements connected to inputs
  • Use validation attributes: required, min, max, pattern
  • Use <fieldset> and <legend> to group related inputs
  • Test with keyboard and screen readers

Tables

Create accessible data tables with proper structure. Continue →

Media

Embed audio, video, and responsive images. Continue →