HTML Forms
advancedPart of HTML Forms & Media
Theory
Forms are the primary way users interact with web applications — they enable login, registration, search, checkout, feedback, and countless other interactions. A form collects user input and sends it to a server for processing.
The <form> Element
The <form> element wraps all input fields and submission controls:
<form action="/submit" method="POST">
<!-- form fields go here -->
</form>Key Attributes
action— the URL where form data is sentmethod— the HTTP method:GETorPOSTenctype— encoding type for file uploads (multipart/form-data)novalidate— disables browser validationtarget— where to display the response (_self,_blank)
GET vs POST
-
GET — appends data to the URL as query parameters (visible in the address bar). Use for searches, filters, and idempotent requests. Limited data size (~2000 characters).
-
POST — sends data in the request body (not visible in the URL). Use for login, registration, file uploads, and any data that changes server state. No size limit.
<!-- GET: search form (data visible in URL) -->
<form action="/search" method="GET">
<input type="text" name="q" placeholder="Search...">
<button type="submit">Search</button>
</form>
<!-- POST: login form (data hidden) -->
<form action="/login" method="POST">
<input type="text" name="username" required>
<input type="password" name="password" required>
<button type="submit">Log In</button>
</form>Input Types
HTML5 offers many type values for <input>:
| Type | Description |
|------|-------------|
| text | Single-line text input |
| password | Masked text input |
| email | Email address with validation |
| number | Numeric input with spin buttons |
| tel | Telephone number |
| url | URL input |
| search | Search field (stylable) |
| date | Date picker |
| time | Time picker |
| color | Color picker |
| file | File upload |
| checkbox | Multiple-choice toggle |
| radio | Single-choice selector |
| range | Slider control |
| hidden | Hidden field (not displayed) |
Other Form Elements
<textarea>— multi-line text input. Userowsandcolsto set dimensions.<select>and<option>— dropdown menus. Use<optgroup>to group options.<button>— clickable button. Types:submit(default),reset,button.<label>— associates text with an input, improving accessibility. Useforattribute matching the input'sid.<fieldset>— groups related fields visually.<legend>— caption for a<fieldset>.
<fieldset>
<legend>Contact Information</legend>
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
</fieldset>Form Validation
HTML5 provides built-in validation attributes:
required— field must be filledminlength/maxlength— text length limitsmin/max— numeric range limitspattern— regex pattern validation (e.g.,pattern="[A-Za-z]+")type="email"— validates email format automatically
Validation happens client-side before submission. Always validate on the server too, as client-side validation can be bypassed.
Accessibility Best Practices
- Every input should have an associated
<label> - Use
fieldsetandlegendfor grouping related inputs - Use
aria-describedbyfor descriptive help text - Use
autocompleteattributes for common fields (name, email, address)
Use autocomplete="off" sparingly. Modern browsers expect autocomplete attributes to help users fill forms faster. Only disable it for sensitive one-time fields like CAPTCHAs.
Practical Examples
Exercises
Create a Login Form
Create a simple login form with username and password fields, a 'Remember Me' checkbox, and a submit button. Use POST method and ensure proper labels.
Expected Output:
A styled login form with username input, password input (masked), a checkbox for 'Remember Me', and a submit button labeled 'Log In'. Both inputs should have the required attribute.Build a Feedback Form
Create a feedback form that includes: name, email, a rating slider (1-10), a category dropdown (Bug Report, Feature Request, General), a message textarea, and a file upload for screenshots. Use fieldset to group the fields.
Expected Output:
A well-structured feedback form with two fieldsets (e.g., 'Contact Info' and 'Feedback Details'). Includes a range input for rating, select dropdown, textarea, and file input.Mini Quiz
Mini Quiz
Mini Project
Mini Project: Registration Form
Build a complete user registration form that collects personal details, account information, and preferences. The form should be well-structured, accessible, and include client-side validation.
Requirements:
Bonus Challenge
Add a password strength indicator using HTML range or meter elements, and add custom error messages using the setCustomValidity() JavaScript API.