Forms & Input
Input Types (text, email, password, number)
Master HTML5 input types for better user experience and built-in validation. Learn all input types, their attributes, and when to use each one.
HTML5 Input Types Overview
HTML5 introduced many specialized input types with built-in validation and mobile-optimized keyboards.
Text Inputs
- text
- tel
- url
- search
- password
Numeric Inputs
- number
- range
Date/Time Inputs
- date
- time
- datetime-local
- month
- week
Selection Inputs
- checkbox
- radio
- file
- color
Buttons
- button
- submit
- reset
- image
Hidden
- hidden
Text Input Types
1. type="text" (Default)
HTML
<label for="username">Username:</label>
<input type="text"
id="username"
name="username"
placeholder="Enter username"
maxlength="50"
required>
<!-- Common attributes: -->
<!-- placeholder: Hint text -->
<!-- maxlength: Maximum character count -->
<!-- minlength: Minimum character count -->
<!-- pattern: Regex validation -->
<!-- required: Field is mandatory -->2. type="email"
HTML
<label for="email">Email:</label>
<input type="email"
id="email"
name="email"
placeholder="you@example.com"
required>
<!-- Benefits: -->
<!-- - Built-in email format validation -->
<!-- - Mobile keyboards show @ and .com -->
<!-- - Multiple emails with 'multiple' attribute -->3. type="password"
HTML
<label for="password">Password:</label>
<input type="password"
id="password"
name="password"
minlength="8"
pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}"
title="Must contain at least one number, one uppercase and lowercase letter, and at least 8 characters"
required>
<!-- Features: -->
<!-- - Text is masked (•••) -->
<!-- - Not saved in browser history -->
<!-- - Autocomplete can be controlled -->4. type="tel"
HTML
<label for="phone">Phone:</label>
<input type="tel"
id="phone"
name="phone"
placeholder="+1 (555) 000-0000"
pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}">
<!-- Benefits: -->
<!-- - Mobile keyboards show numeric keypad -->
<!-- - No built-in validation (use pattern) -->5. type="url"
HTML
<label for="website">Website:</label>
<input type="url"
id="website"
name="website"
placeholder="https://example.com"
required>
<!-- Features: -->
<!-- - Validates URL format -->
<!-- - Mobile keyboards show .com and / -->6. type="search"
HTML
<label for="search">Search:</label>
<input type="search"
id="search"
name="q"
placeholder="Search...">
<!-- Features: -->
<!-- - Shows clear button (×) when typing -->
<!-- - Better semantics for search -->Numeric Input Types
1. type="number"
HTML
<label for="quantity">Quantity:</label>
<input type="number"
id="quantity"
name="quantity"
min="1"
max="100"
step="1"
value="1"
required>
<!-- Attributes: -->
<!-- min: Minimum value -->
<!-- max: Maximum value -->
<!-- step: Increment/decrement amount -->
<!-- value: Default value -->
<!-- Mobile keyboard: Numeric -->2. type="range" (Slider)
HTML
<label for="volume">Volume:</label>
<input type="range"
id="volume"
name="volume"
min="0"
max="100"
step="5"
value="50">
<output for="volume" id="volume-output">50</output>
<script>
const range = document.getElementById('volume');
const output = document.getElementById('volume-output');
range.addEventListener('input', (e) => {
output.textContent = e.target.value;
});
</script>Date and Time Inputs
1. type="date"
HTML
<label for="birthday">Birthday:</label>
<input type="date"
id="birthday"
name="birthday"
min="1900-01-01"
max="2024-12-31"
required>
<!-- Format: YYYY-MM-DD -->
<!-- Opens date picker on click -->2. type="time"
HTML
<label for="appointment">Appointment Time:</label>
<input type="time"
id="appointment"
name="appointment"
min="09:00"
max="18:00"
required>
<!-- Format: HH:MM (24-hour) -->
<!-- Opens time picker -->3. type="datetime-local"
HTML
<label for="meeting">Meeting:</label>
<input type="datetime-local"
id="meeting"
name="meeting"
required>
<!-- Format: YYYY-MM-DDTHH:MM -->
<!-- Date and time in one input -->4. type="month"
HTML
<label for="credit-exp">Card Expiry:</label>
<input type="month"
id="credit-exp"
name="credit-exp"
min="2024-01"
required>
<!-- Format: YYYY-MM -->5. type="week"
HTML
<label for="week">Select Week:</label>
<input type="week"
id="week"
name="week">
<!-- Format: YYYY-W## -->Selection Inputs
1. type="checkbox"
HTML
<input type="checkbox"
id="subscribe"
name="subscribe"
value="yes"
checked>
<label for="subscribe">Subscribe to newsletter</label>
<!-- Multiple checkboxes with same name -->
<fieldset>
<legend>Interests:</legend>
<input type="checkbox" id="html" name="interests" value="html">
<label for="html">HTML</label>
<input type="checkbox" id="css" name="interests" value="css">
<label for="css">CSS</label>
<input type="checkbox" id="js" name="interests" value="js" checked>
<label for="js">JavaScript</label>
</fieldset>2. type="radio"
HTML
<fieldset>
<legend>Choose payment method:</legend>
<input type="radio" id="credit" name="payment" value="credit" checked>
<label for="credit">Credit Card</label>
<input type="radio" id="debit" name="payment" value="debit">
<label for="debit">Debit Card</label>
<input type="radio" id="paypal" name="payment" value="paypal">
<label for="paypal">PayPal</label>
</fieldset>
<!-- All radio buttons with same 'name' = only one selectable -->3. type="file"
HTML
<label for="avatar">Choose profile picture:</label>
<input type="file"
id="avatar"
name="avatar"
accept="image/png, image/jpeg"
required>
<!-- Multiple files -->
<label for="photos">Upload photos:</label>
<input type="file"
id="photos"
name="photos"
accept="image/*"
multiple>
<!-- accept attribute: -->
<!-- image/* - All images -->
<!-- image/png, image/jpeg - Specific types -->
<!-- .pdf, .doc - File extensions -->
<!-- audio/*, video/* - Audio/video files -->4. type="color"
HTML
<label for="favcolor">Favorite Color:</label>
<input type="color"
id="favcolor"
name="favcolor"
value="#ff0000">
<!-- Value must be in hex format (#RRGGBB) -->
<!-- Opens color picker -->Button Types
1. type="submit"
HTML
<input type="submit" value="Submit Form">
<!-- Or -->
<button type="submit">Submit Form</button>
<!-- Submits the form -->2. type="button"
HTML
<input type="button" value="Click Me" onclick="doSomething()">
<!-- Or -->
<button type="button" onclick="doSomething()">Click Me</button>
<!-- Does nothing by default (use JavaScript) -->3. type="reset"
HTML
<input type="reset" value="Clear Form">
<!-- Or -->
<button type="reset">Clear Form</button>
<!-- Resets all form fields to default values -->4. type="image"
HTML
<input type="image"
src="submit-button.png"
alt="Submit"
width="100"
height="40">
<!-- Acts as submit button -->
<!-- Sends x,y coordinates of click -->type="hidden"
HTML
<input type="hidden" name="user_id" value="12345">
<input type="hidden" name="csrf_token" value="abc123xyz">
<!-- Not visible to user -->
<!-- Submitted with form -->
<!-- Use for: IDs, tokens, tracking data -->Common Attributes
Validation Attributes
HTML
<input type="text"
required <!-- Field must be filled -->
minlength="3" <!-- Minimum length -->
maxlength="50" <!-- Maximum length -->
pattern="[A-Za-z]{3,}" <!-- Regex pattern -->
title="Username must be at least 3 letters"> <!-- Tooltip -->Usability Attributes
HTML
<input type="text"
placeholder="Enter text..." <!-- Hint text -->
value="Default value" <!-- Initial value -->
disabled <!-- Cannot interact -->
readonly <!-- Cannot edit -->
autofocus <!-- Focus on page load -->
autocomplete="name" <!-- Autocomplete hint -->
spellcheck="true"> <!-- Enable spellcheck -->Input Type Selection Guide
| Data Type | Use This Input | Why |
|---|---|---|
| Name, Address | text | General text entry |
email | Built-in validation, optimized keyboard | |
| Phone | tel | Numeric keyboard on mobile |
| Password | password | Masked text |
| Age, Quantity | number | Numeric input with controls |
| Birth Date | date | Date picker |
| Yes/No | checkbox | Single toggle |
| One choice | radio | Single selection from group |
| File Upload | file | File selection |
Best Practices
✅ Do
- Use specific input types (email, tel, date)
- Add labels to every input
- Use placeholder for hints, not labels
- Include validation attributes
- Test on mobile devices
- Use autocomplete attributes
- Provide clear error messages
⌠Don't
- Use text for everything
- Forget labels
- Use placeholder as label
- Rely only on client-side validation
- Make required fields unclear
- Disable autocomplete unnecessarily
- Use cryptic error messages