HTML Forms
Forms are the primary way users interact with websites — every login page, registration form, search bar, checkout process, contact form, and survey you have ever used on the internet is an HTML form. Learning to create forms is essential because they enable two-way communication between users and your website. Without forms, the web would be a read-only experience. In this topic, you will learn every form element: text inputs, passwords, emails, checkboxes, radio buttons, dropdowns, text areas, and submit buttons. You will also understand how forms send data to servers and the critical differences between GET and POST methods.
HTML Forms
Forms are containers for interactive input elements that allow users to enter and submit data. The <form> tag wraps all form elements and handles data submission. Key form attributes: • action — The URL where form data is sent when submitted. If omitted, the form submits to the current page. • method — How data is sent: GET (data in URL query string, visible and limited) or POST (data in request body, hidden and unlimited). When to use GET vs POST: • GET: Search forms, filters, pagination — data is in the URL so it can be bookmarked/shared. Limited to ~2000 characters. Data is visible in the address bar. • POST: Login forms, registration, file uploads, payment forms — data is hidden in the request body. No size limit. More secure for sensitive data. Every form input should have: • A name attribute — This is the KEY used to identify the data on the server. Without a name, the input's value will NOT be submitted. • A <label> element — Associates text with an input for accessibility. When users click the label, the input receives focus. Use either wrapping (label around input) or the for/id pairing. • Validation attributes — required (must be filled), pattern (regex validation), min/max (number ranges), minlength/maxlength (text length limits). HTML5 introduced many built-in input types that handle validation automatically: email (validates @ format), url (validates URL format), tel (phone keyboard on mobile), number (numeric keyboard), date (date picker), color (color picker), and more.
Form Elements — Complete Reference
- <form action='url' method='POST'> — Container for all form elements. action = where data goes, method = how data is sent (GET or POST).
- <input type='text'> — Single-line text field. Add placeholder for hint text, maxlength for character limit, required to make it mandatory.
- <input type='password'> — Password field that hides what the user types with dots/asterisks. Never stores passwords — that is the server's job.
- <input type='email'> — Email field with built-in validation. Browsers check for @ and domain format. On mobile, shows a keyboard with @ key.
- <input type='number'> — Numeric input with up/down arrows. Use min, max, and step attributes to control the range and increment.
- <input type='tel'> — Phone number input. On mobile devices, it shows a phone-number keyboard. Does not validate format (use pattern for that).
- <input type='date'> — Shows a date picker UI. No need for JavaScript date libraries! Use min and max to restrict the date range.
- <input type='checkbox'> — Checkboxes allow multiple selections. Each checkbox is independent — users can select zero, one, or all.
- <input type='radio'> — Radio buttons allow only ONE selection from a group. All radios in a group must share the same name attribute.
- <select> and <option> — Dropdown menu. <select> is the container, <option> is each choice. Add multiple attribute to allow multi-select.
- <textarea rows='4' cols='50'> — Multi-line text area for longer content like messages, comments, or descriptions. Resizable by default.
- <button type='submit'> — Submit button that sends the form data. type='reset' clears all fields. type='button' does nothing by default (used with JavaScript).
- <label for='inputId'> — Associates descriptive text with an input. Clicking the label focuses/activates the input. Essential for accessibility.
- <fieldset> and <legend> — Groups related form elements with a visible border and title. Great for organizing complex forms into logical sections.
Form Validation — Built-In Superpowers
HTML5 provides powerful built-in form validation that works without any JavaScript. These validation attributes are checked by the browser when the user clicks the submit button: • required — The field must be filled before submitting. Browser shows an error message automatically. • type='email' — Must contain a valid email format (text@domain.com). • type='url' — Must contain a valid URL (https://...). • minlength='3' / maxlength='50' — Controls text length. • min='0' / max='100' — Controls number range. • pattern='[regex]' — Custom validation using regular expressions. Example: pattern='[0-9]{10}' for a 10-digit phone number. • step='0.01' — Controls increment for number inputs (useful for prices). The browser displays native error messages in the user's language automatically. You can customize these messages with JavaScript if needed. Pro tip: Always validate on the SERVER side too! Client-side validation (HTML/JavaScript) can be bypassed by tech-savvy users. Server-side validation is your last line of defense.
Form Example
<!DOCTYPE html>
<html>
<body>
<h2>Registration Form</h2>
<form>
<p>
<label>Full Name:<br>
<input type="text" placeholder="Enter your name" required
minlength="2" maxlength="50" style="padding:8px;width:250px">
</label>
</p>
<p>
<label>Email Address:<br>
<input type="email" placeholder="you@example.com" required
style="padding:8px;width:250px">
</label>
</p>
<p>
<label>Password:<br>
<input type="password" placeholder="Min 8 characters" required
minlength="8" style="padding:8px;width:250px">
</label>
</p>
<p>
<label>Age:<br>
<input type="number" min="13" max="120" placeholder="Your age"
style="padding:8px;width:100px">
</label>
</p>
<p>
Gender:
<label><input type="radio" name="gender" value="male"> Male</label>
<label><input type="radio" name="gender" value="female"> Female</label>
<label><input type="radio" name="gender" value="other"> Other</label>
</p>
<p>
<label>Favourite Course:
<select>
<option value="">Select a course</option>
<option>HTML</option>
<option>CSS</option>
<option>JavaScript</option>
<option>React</option>
</select>
</label>
</p>
<p>
<label><input type="checkbox" required> I agree to the terms</label>
</p>
<button type="submit" style="padding:10px 24px;background:#04AA6D;color:white;border:none;border-radius:4px;cursor:pointer;font-size:16px">
Register
</button>
</form>
</body>
</html>