Forms and Input Types

Week 1, Day 2 - Lecture 3

Introduction to HTML Forms

Forms are the backbone of user interaction on the web. Think of them as digital versions of paper forms - they collect information from users and send it to a server. Every time you log in, sign up, search, or make a purchase online, you're using a form!

sequenceDiagram participant User participant Browser participant Server User->>Browser: Fills out form User->>Browser: Clicks submit Browser->>Server: Sends form data Server->>Server: Processes data Server->>Browser: Returns response Browser->>User: Shows result

The Form Element

The <form> element is the container for all form controls. It's like an envelope that holds all the information to be sent.

<form action="/submit-form" method="post">
    <!-- Form controls go here -->
</form>

Form Attributes

Attribute Purpose Example
action URL where form data is sent action="/submit"
method HTTP method (GET or POST) method="post"
enctype How data is encoded enctype="multipart/form-data"
target Where to display response target="_blank"
novalidate Disable browser validation novalidate

GET vs POST Methods

graph TD A[Form Methods] --> B[GET] A --> C[POST] B --> D[Data in URL] B --> E[Bookmarkable] B --> F[Limited data size] B --> G[Not secure] C --> H[Data in body] C --> I[Not bookmarkable] C --> J[No size limit] C --> K[More secure] style B fill:#ffd,stroke:#333,stroke-width:2px style C fill:#dfd,stroke:#333,stroke-width:2px

Input Types

HTML5 provides a rich set of input types for different kinds of data. Think of these as specialized tools in a toolbox - each designed for a specific purpose.

Text Inputs

<!-- Basic text input -->
<input type="text" name="username" placeholder="Enter username">

<!-- Password input (characters are hidden) -->
<input type="password" name="password" placeholder="Enter password">

<!-- Email input (with built-in validation) -->
<input type="email" name="email" placeholder="your@email.com">

<!-- URL input -->
<input type="url" name="website" placeholder="https://example.com">

<!-- Search input (may have special styling) -->
<input type="search" name="query" placeholder="Search...">

<!-- Telephone input -->
<input type="tel" name="phone" placeholder="123-456-7890">

Numeric Inputs

<!-- Number input with min/max -->
<input type="number" name="age" min="0" max="120" step="1">

<!-- Range slider -->
<input type="range" name="volume" min="0" max="100" step="10">

Date and Time Inputs

<!-- Date picker -->
<input type="date" name="birthday" min="1900-01-01" max="2024-12-31">

<!-- Time picker -->
<input type="time" name="appointment">

<!-- Date and time -->
<input type="datetime-local" name="meeting">

<!-- Month picker -->
<input type="month" name="expiry">

<!-- Week picker -->
<input type="week" name="vacation">

Color and File Inputs

<!-- Color picker -->
<input type="color" name="favorite_color" value="#ff0000">

<!-- File upload (single file) -->
<input type="file" name="avatar" accept="image/*">

<!-- File upload (multiple files) -->
<input type="file" name="documents" multiple accept=".pdf,.doc,.docx">

Checkboxes and Radio Buttons

<!-- Checkbox (multiple selection) -->
<input type="checkbox" name="interests" value="coding" id="coding">
<label for="coding">Coding</label>

<input type="checkbox" name="interests" value="design" id="design">
<label for="design">Design</label>

<!-- Radio buttons (single selection) -->
<input type="radio" name="gender" value="male" id="male">
<label for="male">Male</label>

<input type="radio" name="gender" value="female" id="female">
<label for="female">Female</label>

<input type="radio" name="gender" value="other" id="other">
<label for="other">Other</label>

Hidden Input

<!-- Hidden field (not visible to user) -->
<input type="hidden" name="user_id" value="12345">

Form Controls

Textarea

<textarea name="message" rows="5" cols="40" placeholder="Enter your message here..."></textarea>

Select Dropdown

<select name="country">
    <option value="">Select a country</option>
    <option value="us">United States</option>
    <option value="ca">Canada</option>
    <option value="uk">United Kingdom</option>
</select>

<!-- Multiple selection -->
<select name="skills" multiple>
    <option value="html">HTML</option>
    <option value="css">CSS</option>
    <option value="js">JavaScript</option>
</select>

<!-- Grouped options -->
<select name="food">
    <optgroup label="Fruits">
        <option value="apple">Apple</option>
        <option value="banana">Banana</option>
    </optgroup>
    <optgroup label="Vegetables">
        <option value="carrot">Carrot</option>
        <option value="broccoli">Broccoli</option>
    </optgroup>
</select>

Datalist (Autocomplete)

<input list="browsers" name="browser">
<datalist id="browsers">
    <option value="Chrome">
    <option value="Firefox">
    <option value="Safari">
    <option value="Edge">
</datalist>

Buttons

<!-- Submit button (submits form) -->
<button type="submit">Submit</button>
<input type="submit" value="Submit">

<!-- Reset button (clears form) -->
<button type="reset">Reset</button>
<input type="reset" value="Reset">

<!-- Regular button (no default action) -->
<button type="button" onclick="doSomething()">Click Me</button>

Form Structure and Accessibility

Labels and Form Association

<!-- Method 1: Wrapping -->
<label>
    Username:
    <input type="text" name="username">
</label>

<!-- Method 2: For attribute -->
<label for="email-field">Email:</label>
<input type="email" id="email-field" name="email">

Fieldset and Legend

<fieldset>
    <legend>Personal Information</legend>
    
    <label for="fname">First Name:</label>
    <input type="text" id="fname" name="fname">
    
    <label for="lname">Last Name:</label>
    <input type="text" id="lname" name="lname">
</fieldset>

<fieldset>
    <legend>Choose your interests</legend>
    
    <input type="checkbox" id="coding" name="interest" value="coding">
    <label for="coding">Coding</label>
    
    <input type="checkbox" id="music" name="interest" value="music">
    <label for="music">Music</label>
</fieldset>

Form Validation

HTML5 provides built-in validation that works without JavaScript. It's like having a spell-checker for form inputs!

Required Fields

<input type="text" name="username" required>
<input type="email" name="email" required>

Pattern Validation

<!-- Password: 8+ characters, at least one number and one letter -->
<input type="password" 
       pattern="^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$"
       title="Must contain at least 8 characters, including letters and numbers">

<!-- Phone number pattern -->
<input type="tel" 
       pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}"
       placeholder="123-456-7890">

Length Constraints

<!-- Text length -->
<input type="text" minlength="3" maxlength="20">

<!-- Number range -->
<input type="number" min="18" max="100">

<!-- Date range -->
<input type="date" min="2024-01-01" max="2024-12-31">
graph TD A[Form Validation] --> B[Required] A --> C[Pattern] A --> D[Min/Max] A --> E[Type-specific] B --> F[required attribute] C --> G[pattern attribute] D --> H[min/max attributes] D --> I[minlength/maxlength] E --> J[email format] E --> K[URL format] E --> L[number range] style A fill:#f9f,stroke:#333,stroke-width:2px style B fill:#9cf,stroke:#333,stroke-width:2px style C fill:#fc9,stroke:#333,stroke-width:2px style D fill:#cf9,stroke:#333,stroke-width:2px style E fill:#c9f,stroke:#333,stroke-width:2px

Advanced Form Features

Autocomplete

<!-- Enable autocomplete for common fields -->
<input type="text" name="fname" autocomplete="given-name">
<input type="text" name="lname" autocomplete="family-name">
<input type="email" name="email" autocomplete="email">
<input type="tel" name="phone" autocomplete="tel">

<!-- Disable autocomplete for sensitive fields -->
<input type="password" name="password" autocomplete="new-password">
<input type="text" name="cc-number" autocomplete="off">

Form Autofocus

<!-- Focus this field when page loads -->
<input type="text" name="search" autofocus>

Disabled and Readonly

<!-- Disabled (cannot be edited or submitted) -->
<input type="text" name="username" value="john_doe" disabled>

<!-- Readonly (cannot be edited but will be submitted) -->
<input type="text" name="user_id" value="12345" readonly>

Complete Form Examples

Login Form

<form action="/login" method="post">
    <h2>Login</h2>
    
    <div>
        <label for="login-email">Email:</label>
        <input type="email" id="login-email" name="email" required autofocus>
    </div>
    
    <div>
        <label for="login-password">Password:</label>
        <input type="password" id="login-password" name="password" required>
    </div>
    
    <div>
        <input type="checkbox" id="remember" name="remember">
        <label for="remember">Remember me</label>
    </div>
    
    <button type="submit">Login</button>
    <a href="/forgot-password">Forgot password?</a>
</form>

Registration Form

<form action="/register" method="post" enctype="multipart/form-data">
    <h2>Create Account</h2>
    
    <fieldset>
        <legend>Personal Information</legend>
        
        <div>
            <label for="reg-fname">First Name:</label>
            <input type="text" id="reg-fname" name="fname" required>
        </div>
        
        <div>
            <label for="reg-lname">Last Name:</label>
            <input type="text" id="reg-lname" name="lname" required>
        </div>
        
        <div>
            <label for="reg-dob">Date of Birth:</label>
            <input type="date" id="reg-dob" name="dob" required>
        </div>
    </fieldset>
    
    <fieldset>
        <legend>Account Details</legend>
        
        <div>
            <label for="reg-email">Email:</label>
            <input type="email" id="reg-email" name="email" required>
        </div>
        
        <div>
            <label for="reg-password">Password:</label>
            <input type="password" id="reg-password" name="password" 
                   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>
        </div>
        
        <div>
            <label for="reg-confirm">Confirm Password:</label>
            <input type="password" id="reg-confirm" name="confirm_password" required>
        </div>
    </fieldset>
    
    <fieldset>
        <legend>Preferences</legend>
        
        <div>
            <label for="reg-avatar">Profile Picture:</label>
            <input type="file" id="reg-avatar" name="avatar" accept="image/*">
        </div>
        
        <div>
            <label for="reg-newsletter">Subscribe to newsletter:</label>
            <input type="checkbox" id="reg-newsletter" name="newsletter" checked>
        </div>
    </fieldset>
    
    <div>
        <input type="checkbox" id="terms" name="terms" required>
        <label for="terms">I agree to the <a href="/terms">terms and conditions</a></label>
    </div>
    
    <button type="submit">Create Account</button>
    <button type="reset">Clear Form</button>
</form>

Contact Form

<form action="/contact" method="post">
    <h2>Contact Us</h2>
    
    <div>
        <label for="contact-name">Name:</label>
        <input type="text" id="contact-name" name="name" required>
    </div>
    
    <div>
        <label for="contact-email">Email:</label>
        <input type="email" id="contact-email" name="email" required>
    </div>
    
    <div>
        <label for="contact-subject">Subject:</label>
        <select id="contact-subject" name="subject" required>
            <option value="">Select a subject</option>
            <option value="general">General Inquiry</option>
            <option value="support">Technical Support</option>
            <option value="billing">Billing Question</option>
            <option value="feedback">Feedback</option>
        </select>
    </div>
    
    <div>
        <label for="contact-priority">Priority:</label>
        <div>
            <input type="radio" id="priority-low" name="priority" value="low" checked>
            <label for="priority-low">Low</label>
            
            <input type="radio" id="priority-medium" name="priority" value="medium">
            <label for="priority-medium">Medium</label>
            
            <input type="radio" id="priority-high" name="priority" value="high">
            <label for="priority-high">High</label>
        </div>
    </div>
    
    <div>
        <label for="contact-message">Message:</label>
        <textarea id="contact-message" name="message" rows="5" required></textarea>
    </div>
    
    <div>
        <label for="contact-attachment">Attachment:</label>
        <input type="file" id="contact-attachment" name="attachment">
    </div>
    
    <button type="submit">Send Message</button>
</form>

Best Practices

mindmap root((Form Best Practices)) Accessibility Always use labels Logical tab order Clear error messages ARIA attributes User Experience Clear field labeling Helpful placeholders Inline validation Progress indicators Security HTTPS for sensitive data CSRF protection Input sanitization Proper authentication Performance Minimize round trips Client-side validation Efficient file uploads Progressive enhancement

Do's and Don'ts

Assignment: Create a Multi-Part Form

  1. Create a job application form with:
    • Personal information section (name, email, phone)
    • Education section (degree, school, graduation date)
    • Experience section (previous jobs, skills)
    • Additional information (cover letter, resume upload)
  2. Include at least:
    • 5 different input types
    • Proper fieldsets and legends
    • Required field validation
    • Pattern validation for phone/email
    • File upload for resume
    • Radio buttons and checkboxes
  3. Create a survey form with:
    • Rating scales (radio buttons)
    • Multiple choice (checkboxes)
    • Open-ended questions (textarea)
    • Demographic information
  4. Test all forms in different browsers
  5. Validate your HTML using W3C validator

Bonus: Style your forms with basic CSS to make them visually appealing!