š HTML5 Forms ā Complete Guide for Modern Web Forms
HTML5 introduced powerful new input types, validation features, and attributes that make forms more user-friendly, accessible, and interactive ā without JavaScript.
š With HTML5, you can validate, format, and improve forms natively!
š§± Basic HTML Form Structure
html
<form action="/submit" method="POST">
<label for="name">Name:</label>
<input type="text" id="name" name="name" required>
<input type="submit" value="Submit">
</form>
š¹ HTML5 Input Types
Type | Purpose |
---|---|
text | Regular text input |
email | Validates email format |
url | Validates URL format |
tel | Telephone numbers |
number | Numeric input |
range | Slider (min-max range) |
date , time , datetime-local | Date/time pickers |
color | Color picker |
search | Search input |
file | Upload files |
checkbox , radio | Multiple/single choices |
password | Hidden input |
hidden | Invisible field |
Ā
ā Example:
html
<input type="email" name="user_email" required>
ā Built-In Validation
HTML5 lets you validate inputs without JavaScript:
Attribute | Description |
---|---|
required | Field must be filled |
min , max | Limit number/date input |
minlength , maxlength | Character limits |
pattern | Regex pattern |
step | Valid step interval (for numbers/dates) |
type="email" | Automatically checks email format |
Ā
html
<input type="text" required minlength="3" maxlength="20">
<input type="email" required>
<input type="number" min="18" max="99">
<input type="text" pattern="[A-Za-z]{3,}" title="Only letters, min 3">
š§ Useful Form Attributes
Attribute | Description |
---|---|
placeholder | Placeholder text |
autofocus | Focus on load |
autocomplete | Suggest saved values |
readonly | Non-editable field |
disabled | Greyed-out and skipped |
multiple | Select multiple options (e.g., in file or select) |
Ā
š¦ Grouping Inputs
<fieldset>
& <legend>
html
<fieldset>
<legend>Personal Info</legend>
<input type="text" name="name">
<input type="email" name="email">
</fieldset>
š¤ File Upload Input
html
<input type="file" name="resume" accept=".pdf,.docx" required>
<input type="file" name="gallery" accept="image/*" multiple>
š Radio & Checkbox
html
<!-- Radio (1 choice) -->
<input type="radio" name="gender" value="male"> Male
<input type="radio" name="gender" value="female"> Female
<!-- Checkbox (multiple) -->
<input type="checkbox" name="skills" value="html"> HTML
<input type="checkbox" name="skills" value="css"> CSS
šØ Styling Forms (Tip)
Forms are fully customizable with CSS:
css
input:invalid {
border-color: red;
}
input:valid {
border-color: green;
}
š datalist
for Input Suggestions
html
<input list="languages" name="language">
<datalist id="languages">
<option value="HTML">
<option value="CSS">
<option value="JavaScript">
</datalist>
š§Ŗ Form Submission Handling
html
<form action="/submit" method="POST">
<input type="text" name="username">
<input type="submit" value="Send">
</form>
Use method="GET"
for query strings (e.g., search)
Use method="POST"
to send form data securely
š Learn More
ā Summary
Feature | HTML5 Form Support |
---|---|
New input types | ā |
Built-in validation | ā |
Accessibility improvements | ā |
Native pickers (date, color) | ā |
Form styling with CSS | ā |