Welcome to Day 12 of the 180 Days of Frontend Development Challenge. Today we'll master form creation using proper HTML structure, accessible inputs, and professional patterns. For comprehensive form techniques, see the Learn Frontend Development in 180 Days ebook.
Form Fundamentals
1. Basic Form Structure
<form action="/submit" method="POST">
<fieldset>
<legend>Contact Information</legend>
<div class="form-group">
<label for="name">Full Name:</label>
<input type="text" id="name" name="name" required>
</div>
<button type="submit">Send</button>
</fieldset>
</form>
Key Elements:
-
<form>
: Container for all form elements -
<fieldset>
: Groups related inputs -
<legend>
: Describes the fieldset -
<label>
: Identifies each input -
<input>
: Data entry field -
<button>
: Form submission
Input Types Every Developer Should Know
1. Text Inputs
<!-- Standard Text -->
<label for="username">Username:</label>
<input type="text" id="username" name="username" minlength="4" maxlength="20">
<!-- Email Validation -->
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<!-- Password Field -->
<label for="pwd">Password:</label>
<input type="password" id="pwd" name="password" required>
2. Specialized Inputs
<!-- Telephone -->
<label for="phone">Mobile:</label>
<input type="tel" id="phone" name="phone" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}">
<!-- Date Picker -->
<label for="birthdate">Birth Date:</label>
<input type="date" id="birthdate" name="birthdate">
<!-- Color Picker -->
<label for="color">Theme Color:</label>
<input type="color" id="color" name="color" value="#0066cc">
<!-- File Upload -->
<label for="resume">Upload Resume:</label>
<input type="file" id="resume" name="resume" accept=".pdf,.doc,.docx">
Professional Textarea Implementation
<label for="message">Your Message:</label>
<textarea id="message" name="message" rows="5" cols="50"
placeholder="Enter your message here..."
minlength="10" maxlength="500" required></textarea>
Key Attributes:
-
rows
: Visible height -
cols
: Visible width -
minlength/maxlength
: Character limits -
placeholder
: Example text -
required
: Mandatory field
Complete Contact Form Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Professional Contact Form</title>
<style>
form {
max-width: 600px;
margin: 2rem auto;
padding: 2rem;
border: 1px solid #ddd;
border-radius: 8px;
}
.form-group {
margin-bottom: 1.5rem;
}
label {
display: block;
margin-bottom: 0.5rem;
font-weight: bold;
}
input[type="text"],
input[type="email"],
textarea {
width: 100%;
padding: 0.75rem;
border: 1px solid #ddd;
border-radius: 4px;
}
button {
background: #0066cc;
color: white;
padding: 0.75rem 1.5rem;
border: none;
border-radius: 4px;
cursor: pointer;
}
</style>
</head>
<body>
<form action="/contact" method="POST">
<fieldset>
<legend>Contact Us</legend>
<div class="form-group">
<label for="name">Full Name*</label>
<input type="text" id="name" name="name" required
placeholder="John Doe" minlength="2">
</div>
<div class="form-group">
<label for="email">Email*</label>
<input type="email" id="email" name="email" required
placeholder="your@email.com">
</div>
<div class="form-group">
<label for="phone">Phone</label>
<input type="tel" id="phone" name="phone"
placeholder="123-456-7890" pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}">
</div>
<div class="form-group">
<label for="subject">Subject</label>
<select id="subject" name="subject">
<option value="">Select a subject</option>
<option value="support">Technical Support</option>
<option value="sales">Sales Inquiry</option>
</select>
</div>
<div class="form-group">
<label for="message">Message*</label>
<textarea id="message" name="message" rows="6" required
placeholder="Your message here..."></textarea>
</div>
<div class="form-group">
<label>
<input type="checkbox" name="subscribe" checked>
Subscribe to newsletter
</label>
</div>
<button type="submit">Send Message</button>
</fieldset>
</form>
</body>
</html>
Professional Form Practices
-
Accessibility Essentials
- Always pair
<label>
withfor
andid
- Use
fieldset
andlegend
for grouped inputs - Include proper ARIA attributes when needed
- Always pair
Validation Patterns
<!-- ZIP Code Validation -->
<input type="text" pattern="\d{5}(-\d{4})?"
title="5 or 9 digit ZIP code">
<!-- Password Requirements -->
<input type="password" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}"
title="8+ chars with uppercase, lowercase & number">
-
Mobile Optimization
- Use appropriate
inputmode
attributes - Set correct
autocomplete
values - Implement responsive sizing
- Use appropriate
Exercises
-
Build a Registration Form with:
- Text inputs (name, username)
- Email validation
- Password requirements
- Terms agreement checkbox
- Submit button
-
Create a Survey Form including:
- Radio button options
- Checkbox selections
- Rating scale (1-5)
- Comment textarea
Fix These Form Errors:
<form>
<div>Enter your name</div>
<input id="name">
<button>Submit</button>
</form>
What's Next?
Tomorrow (Day 13) covers Form Validation - client-side techniques, error messaging, and accessibility. For advanced form patterns including multi-step wizards and file uploads, see Chapter 8 in the Learn Frontend Development in 180 Days ebook.
Pro Tip: Always test forms using keyboard navigation and screen readers to verify accessibility compliance.
Airdrop alert! DEV Contributor rewards now live for Dev.to contributors as a thank-you for your contributions! Visit the claim page here (limited supply — act fast). – Dev.to Airdrop Desk