Week 2: HTML Forms & Validation
HTML | Forms, Input Types, Validation Attributes
Web Development 1
WEEK 02
1. The Form Element
HTML forms are the primary way users interact with websites. They allow users to enter data that is sent to a server for processing. Every login page, registration form, search bar, and checkout page uses HTML forms.
Basic Form Structure
<form action="/submit" method="POST">
<!-- Form controls go here -->
<label for="username">Username:</label>
<input type="text" id="username" name="username">
<button type="submit">Submit</button>
</form>
Form Attributes
| Attribute |
Description |
Example |
action |
URL where form data is sent |
action="/login" |
method |
HTTP method: GET or POST |
method="POST" |
enctype |
Encoding type (needed for file uploads) |
enctype="multipart/form-data" |
autocomplete |
Enable/disable browser autocomplete |
autocomplete="off" |
novalidate |
Disable browser validation |
novalidate |
GET vs. POST
When to Use GET
- For search forms and filters
- Data appears in the URL:
?name=value&age=25
- Can be bookmarked
- Limited data size (URL length limit)
- NOT for sensitive data (passwords, personal info)
When to Use POST
- For login, registration, payment forms
- Data is sent in the request body (not visible in URL)
- No data size limit
- More secure for sensitive data
- Required for file uploads
<!-- GET method - for search -->
<form action="/search" method="GET">
<input type="text" name="query" placeholder="Search...">
<button type="submit">Search</button>
</form>
<!-- URL becomes: /search?query=html+tutorial -->
<!-- POST method - for login -->
<form action="/login" method="POST">
<input type="email" name="email">
<input type="password" name="password">
<button type="submit">Login</button>
</form>
2. Text Inputs
The <input> element is the most versatile form control. By changing its type attribute, you get completely different controls.
All Input Types
| Type |
Description |
Example Use |
text |
Single-line text input |
Name, username |
email |
Email address (validates format) |
Email field |
password |
Masked text input |
Password field |
number |
Numeric input with spinners |
Age, quantity |
tel |
Telephone number |
Phone number |
url |
URL input (validates format) |
Website address |
date |
Date picker |
Birthday, appointment |
time |
Time picker |
Meeting time |
color |
Color picker |
Theme color |
range |
Slider control |
Volume, rating |
file |
File upload |
Profile picture |
search |
Search input (with clear button) |
Search bar |
hidden |
Hidden field (not visible) |
User ID, tokens |
Text Input: type="text"
<!-- Basic text input -->
<label for="fname">First Name:</label>
<input type="text" id="fname" name="first_name" placeholder="Enter your first name">
Email Input: type="email"
<!-- Email - browser validates the format -->
<label for="email">Email:</label>
<input type="email" id="email" name="email" placeholder="you@example.com">
<!-- If user enters "abc" without @, browser shows an error -->
Password Input: type="password"
<!-- Password - characters are hidden -->
<label for="pass">Password:</label>
<input type="password" id="pass" name="password" minlength="8" placeholder="Min 8 characters">
Number Input: type="number"
<!-- Number with min, max, and step -->
<label for="age">Age:</label>
<input type="number" id="age" name="age" min="18" max="100" step="1" value="20">
<!-- Price with decimal -->
<label for="price">Price ($):</label>
<input type="number" id="price" name="price" min="0" step="0.01">
Tel Input: type="tel"
<!-- Phone number -->
<label for="phone">Phone:</label>
<input type="tel" id="phone" name="phone" placeholder="+970-xxx-xxx-xxxx"
pattern="[0-9+\-]{10,15}">
Date & Time Inputs
<!-- Date picker -->
<label for="dob">Date of Birth:</label>
<input type="date" id="dob" name="date_of_birth" min="1990-01-01" max="2010-12-31">
<!-- Time picker -->
<label for="meeting">Meeting Time:</label>
<input type="time" id="meeting" name="meeting_time">
<!-- Date and time combined -->
<label for="appointment">Appointment:</label>
<input type="datetime-local" id="appointment" name="appointment">
<!-- Month picker -->
<label for="start">Start Month:</label>
<input type="month" id="start" name="start_month">
Color Input: type="color"
<!-- Color picker -->
<label for="favcolor">Favorite Color:</label>
<input type="color" id="favcolor" name="fav_color" value="#00bcd4">
Range Input: type="range"
<!-- Slider -->
<label for="volume">Volume:</label>
<input type="range" id="volume" name="volume" min="0" max="100" value="50" step="10">
<!-- Rating slider -->
<label for="rating">Rating (1-10):</label>
<input type="range" id="rating" name="rating" min="1" max="10" value="5">
File Input: type="file"
<!-- Single file upload -->
<label for="avatar">Profile Picture:</label>
<input type="file" id="avatar" name="avatar" accept="image/*">
<!-- Multiple files -->
<label for="photos">Upload Photos:</label>
<input type="file" id="photos" name="photos" accept="image/*" multiple>
<!-- Specific file types -->
<label for="resume">Resume (PDF):</label>
<input type="file" id="resume" name="resume" accept=".pdf,.doc,.docx">
File Upload Requires enctype
When your form includes file uploads, you must add enctype="multipart/form-data" to the <form> tag and use method="POST".
<form action="/upload" method="POST" enctype="multipart/form-data">
<input type="file" name="photo">
<button type="submit">Upload</button>
</form>
URL Input: type="url"
<!-- URL - validates that it starts with http:// or https:// -->
<label for="website">Website:</label>
<input type="url" id="website" name="website" placeholder="https://example.com">
3. Input Attributes
Attributes modify the behavior and appearance of input elements. Here are the most important ones.
| Attribute |
Description |
Example |
placeholder |
Hint text shown when empty |
placeholder="Enter name" |
value |
Default/initial value |
value="Ahmed" |
name |
Key name sent to server |
name="username" |
id |
Unique identifier (for labels/CSS/JS) |
id="user-name" |
required |
Must be filled before submit |
required |
readonly |
Cannot be edited (still submitted) |
readonly |
disabled |
Cannot be edited or submitted |
disabled |
autofocus |
Automatically focused on page load |
autofocus |
maxlength |
Maximum characters allowed |
maxlength="50" |
minlength |
Minimum characters required |
minlength="3" |
min / max |
Min/max for number, date, range |
min="0" max="100" |
step |
Increment step for number/range |
step="5" |
pattern |
Regex pattern for validation |
pattern="[A-Za-z]{3,}" |
Examples of All Attributes
<!-- placeholder: ghost text hint -->
<input type="text" placeholder="Enter your full name">
<!-- value: pre-filled default -->
<input type="text" value="Palestine">
<!-- required: must be filled -->
<input type="email" name="email" required>
<!-- readonly: user can see but not edit -->
<input type="text" value="STU-2025-001" readonly>
<!-- disabled: grayed out, cannot interact -->
<input type="text" value="Not available" disabled>
<!-- autofocus: cursor starts here -->
<input type="text" name="search" autofocus>
<!-- Combining attributes -->
<input
type="text"
id="username"
name="username"
placeholder="Choose a username"
required
minlength="3"
maxlength="20"
autofocus
>
name vs. id
name is what gets sent to the server: ?username=Ahmed
id is used for labels, CSS selectors, and JavaScript
- They can have the same value, but serve different purposes
- Always include
name on form controls (otherwise data will not be sent)
4. Radio Buttons & Checkboxes
Radio Buttons: type="radio"
Radio buttons allow the user to select one option from a group. All radio buttons in a group must share the same name attribute.
<!-- Radio group - only one can be selected -->
<p>Gender:</p>
<input type="radio" id="male" name="gender" value="male">
<label for="male">Male</label>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label>
<!-- Pre-selected radio with "checked" -->
<p>Preferred Language:</p>
<input type="radio" id="arabic" name="language" value="arabic" checked>
<label for="arabic">Arabic</label>
<input type="radio" id="english" name="language" value="english">
<label for="english">English</label>
<input type="radio" id="french" name="language" value="french">
<label for="french">French</label>
Checkboxes: type="checkbox"
Checkboxes allow users to select multiple options. Each checkbox is independent, but related ones can share the same name.
<!-- Multiple checkboxes -->
<p>Interests:</p>
<input type="checkbox" id="coding" name="interests" value="coding">
<label for="coding">Coding</label><br>
<input type="checkbox" id="design" name="interests" value="design">
<label for="design">Design</label><br>
<input type="checkbox" id="gaming" name="interests" value="gaming" checked>
<label for="gaming">Gaming</label><br>
<input type="checkbox" id="reading" name="interests" value="reading">
<label for="reading">Reading</label>
<!-- Single checkbox - for agreements -->
<input type="checkbox" id="terms" name="terms" required>
<label for="terms">I agree to the Terms and Conditions</label>
Radio vs. Checkbox
| Feature |
Radio |
Checkbox |
| Selection |
One only |
Multiple allowed |
| Shape |
Circle |
Square |
| Same name |
Required (for grouping) |
Optional |
| Use case |
Gender, size, plan |
Interests, features, terms |
5. Select Dropdown
The <select> element creates a dropdown menu. Each choice is defined with an <option> element.
Basic Select
<label for="city">City:</label>
<select id="city" name="city">
<option value="">-- Select a city --</option>
<option value="gaza">Gaza</option>
<option value="khan-younis">Khan Younis</option>
<option value="rafah">Rafah</option>
<option value="deir-al-balah">Deir al-Balah</option>
<option value="jabalia">Jabalia</option>
</select>
Select with Pre-selected Option
<select name="country">
<option value="ps" selected>Palestine</option>
<option value="jo">Jordan</option>
<option value="eg">Egypt</option>
</select>
Option Groups: <optgroup>
<label for="course">Course:</label>
<select id="course" name="course">
<optgroup label="Computer Science">
<option value="web-dev">Web Development</option>
<option value="databases">Database Systems</option>
<option value="algorithms">Algorithms</option>
</optgroup>
<optgroup label="Mathematics">
<option value="calculus">Calculus</option>
<option value="statistics">Statistics</option>
</optgroup>
</select>
Multiple Selection & Size
<!-- Allow multiple selections (hold Ctrl) -->
<label for="skills">Skills (select multiple):</label>
<select id="skills" name="skills" multiple size="5">
<option value="html">HTML</option>
<option value="css">CSS</option>
<option value="js">JavaScript</option>
<option value="python">Python</option>
<option value="php">PHP</option>
</select>
6. Textarea
The <textarea> element creates a multi-line text input area, perfect for comments, messages, and descriptions.
<!-- Basic textarea -->
<label for="message">Message:</label>
<textarea id="message" name="message" rows="5" cols="40"
placeholder="Type your message here..."></textarea>
<!-- Textarea with default text -->
<label for="bio">Bio:</label>
<textarea id="bio" name="bio" rows="4" cols="50">Write about yourself...</textarea>
<!-- Textarea with character limits -->
<label for="review">Review (max 500 chars):</label>
<textarea id="review" name="review" rows="6" cols="50"
maxlength="500" required></textarea>
Textarea vs. Input
<input type="text"> is for single-line text
<textarea> is for multi-line text
<textarea> uses rows and cols for sizing, not width/height
- Default content goes between the tags, not in a
value attribute
7. Buttons
Forms need buttons for submission and interaction. There are multiple ways to create buttons.
<!-- Submit button - submits the form -->
<button type="submit">Submit Form</button>
<!-- Reset button - clears all form fields -->
<button type="reset">Reset Form</button>
<!-- Generic button - does nothing by default (use JavaScript) -->
<button type="button">Click Me</button>
Input Buttons (Older Style)
<!-- These also work, but <button> is preferred -->
<input type="submit" value="Submit">
<input type="reset" value="Reset">
<input type="button" value="Click">
Why Use <button> Over <input>?
<button> can contain HTML (text, images, icons)
<input type="submit"> can only contain plain text via value
<button> is more flexible and easier to style
8. Labels & Accessibility
The <label> element is essential for form accessibility. It tells the user what each form control is for, and clicking the label focuses the associated input.
Method 1: Using for Attribute
<!-- The "for" attribute must match the input's "id" -->
<label for="email">Email Address:</label>
<input type="email" id="email" name="email">
<label for="password">Password:</label>
<input type="password" id="password" name="password">
Method 2: Wrapping the Input
<!-- Wrap the input inside the label - no for/id needed -->
<label>
Email Address:
<input type="email" name="email">
</label>
<label>
Password:
<input type="password" name="password">
</label>
Always Use Labels!
Every form input must have a label. This is critical for:
- Screen readers: They read the label aloud to visually impaired users.
- Click area: Clicking the label focuses the input (helpful on mobile).
- Usability: Labels make forms easier to understand and use.
9. Fieldset & Legend
The <fieldset> element groups related form controls, and <legend> provides a caption for the group.
<form action="/register" method="POST">
<!-- Group 1: Personal Information -->
<fieldset>
<legend>Personal Information</legend>
<label for="fname">First Name:</label>
<input type="text" id="fname" name="first_name" required><br><br>
<label for="lname">Last Name:</label>
<input type="text" id="lname" name="last_name" required><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
</fieldset>
<!-- Group 2: Account Settings -->
<fieldset>
<legend>Account Settings</legend>
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required minlength="8">
</fieldset>
<button type="submit">Register</button>
</form>
10. HTML5 Validation
HTML5 provides built-in form validation. The browser can check if fields are filled, if the format is correct, and if values are within limits — all without JavaScript!
Validation Attributes
| Attribute |
Description |
Example |
required |
Field must be filled |
required |
minlength |
Minimum number of characters |
minlength="3" |
maxlength |
Maximum number of characters |
maxlength="100" |
min |
Minimum value (number/date) |
min="18" |
max |
Maximum value (number/date) |
max="100" |
pattern |
Regex pattern to match |
pattern="[0-9]{5}" |
type |
Built-in format validation |
type="email" checks for @ |
Complete Validation Example
<form action="/register" method="POST">
<!-- Required text with min/max length -->
<label for="name">Full Name (3-50 characters):</label><br>
<input type="text" id="name" name="name"
required minlength="3" maxlength="50"><br><br>
<!-- Email validation (built-in) -->
<label for="email">Email:</label><br>
<input type="email" id="email" name="email" required><br><br>
<!-- Password with minimum length -->
<label for="password">Password (min 8 characters):</label><br>
<input type="password" id="password" name="password"
required minlength="8"><br><br>
<!-- Phone with pattern -->
<label for="phone">Phone (format: 059-xxx-xxxx):</label><br>
<input type="tel" id="phone" name="phone"
pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}"
title="Format: 059-123-4567"><br><br>
<!-- Age with min/max -->
<label for="age">Age (18-65):</label><br>
<input type="number" id="age" name="age"
required min="18" max="65"><br><br>
<!-- URL validation -->
<label for="website">Website:</label><br>
<input type="url" id="website" name="website"
placeholder="https://example.com"><br><br>
<!-- Username pattern: letters and numbers only -->
<label for="username">Username (letters and numbers only):</label><br>
<input type="text" id="username" name="username"
required pattern="[A-Za-z0-9]{3,20}"
title="3-20 characters, letters and numbers only"><br><br>
<!-- Terms checkbox must be checked -->
<input type="checkbox" id="terms" name="terms" required>
<label for="terms">I agree to the Terms</label><br><br>
<button type="submit">Register</button>
<button type="reset">Clear</button>
</form>
The pattern Attribute (Regular Expressions)
The pattern attribute uses regular expressions (regex) to define custom validation rules.
| Pattern |
Description |
Matches |
[A-Za-z]+ |
Letters only |
Ahmed, Sara |
[0-9]{5} |
Exactly 5 digits |
12345, 00000 |
[A-Za-z0-9]{3,15} |
3-15 alphanumeric chars |
user123 |
\d{3}-\d{3}-\d{4} |
Phone format XXX-XXX-XXXX |
059-123-4567 |
The title Attribute for Custom Messages
Use the title attribute alongside pattern to provide a helpful error message when validation fails.
<input type="text" pattern="[A-Za-z]{2,}"
title="Please enter at least 2 letters (no numbers)">
11. Datalist for Autocomplete
The <datalist> element provides a list of predefined options that appear as the user types. Unlike <select>, the user can also type a custom value.
<!-- Input with datalist suggestions -->
<label for="browser">Favorite Browser:</label>
<input type="text" id="browser" name="browser" list="browsers">
<datalist id="browsers">
<option value="Chrome">
<option value="Firefox">
<option value="Safari">
<option value="Edge">
<option value="Opera">
</datalist>
<!-- Datalist for programming languages -->
<label for="lang">Programming Language:</label>
<input type="text" id="lang" name="language" list="languages">
<datalist id="languages">
<option value="JavaScript">
<option value="Python">
<option value="Java">
<option value="C++">
<option value="PHP">
<option value="Ruby">
<option value="Go">
<option value="Rust">
</datalist>
Datalist vs. Select
| Feature |
Datalist |
Select |
| Custom input |
Yes (user can type anything) |
No (must choose from options) |
| Autocomplete |
Yes (filters as you type) |
No |
| Appearance |
Text input with suggestions |
Dropdown menu |
Complete Form Example
Here is a full form combining everything we have learned in this lesson:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Registration Form</title>
</head>
<body>
<h1>Student Registration</h1>
<form action="/register" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>Personal Info</legend>
<label for="fullname">Full Name:</label><br>
<input type="text" id="fullname" name="fullname" required
minlength="3" maxlength="50"><br><br>
<label for="email">Email:</label><br>
<input type="email" id="email" name="email" required><br><br>
<label for="dob">Date of Birth:</label><br>
<input type="date" id="dob" name="dob" required><br><br>
<label>Gender:</label><br>
<input type="radio" id="male" name="gender" value="male" required>
<label for="male">Male</label>
<input type="radio" id="female" name="gender" value="female">
<label for="female">Female</label>
</fieldset>
<fieldset>
<legend>Academic Info</legend>
<label for="major">Major:</label><br>
<select id="major" name="major" required>
<option value="">-- Select Major --</option>
<option value="cs">Computer Science</option>
<option value="it">Information Technology</option>
<option value="mis">Management Info Systems</option>
</select><br><br>
<label for="photo">Profile Photo:</label><br>
<input type="file" id="photo" name="photo" accept="image/*">
</fieldset>
<fieldset>
<legend>Additional</legend>
<label for="bio">Tell us about yourself:</label><br>
<textarea id="bio" name="bio" rows="4" cols="50"
maxlength="300"></textarea><br><br>
<input type="checkbox" id="newsletter" name="newsletter">
<label for="newsletter">Subscribe to newsletter</label><br>
<input type="checkbox" id="terms" name="terms" required>
<label for="terms">I agree to Terms & Conditions</label>
</fieldset>
<br>
<button type="submit">Register</button>
<button type="reset">Clear Form</button>
</form>
</body>
</html>