USD ($)
$
United States Dollar
Euro Member Countries
India Rupee

Forms, Inputs, and Validation Attributes

Lesson 2/30 | Study Time: 27 Min

Forms are the backbone of user interaction on the web—think login screens, contact pages, and e-commerce checkouts. 

HTML Form Basics

Every form starts with the <form> element, which wraps inputs and defines submission behavior. HTML5 forms support native validation, HTTP methods, and accessibility features right out of the box. Let's build from the ground up.


Creating a Basic Form Structure

Use <form> with essential attributes to control data flow and submission.



Practical Example:

xml
<form action="/contact" method="POST" novalidate>
<!-- inputs here -->
<button type="submit">Send Message</button>
</form>


Key Buttons:


1. type="submit": Triggers form send.

2. type="reset": Clears all fields.

3. type="button": Generic, no form action.


Grouping Form Fields with Fieldset


1. Organize related inputs using <fieldset> and <legend> for screen reader clarity.

2. Wrap grouped fields in <fieldset>.

3. Add <legend> as the group title.

4. Improves accessibility and visual structure.


Example: Shipping Form

xml
<fieldset>
<legend>Shipping Address</legend>
<label for="street">Street: <input id="street" name="street"></label>
</fieldset>

Input Types and Attributes

HTML5 offers 24+ input types for specific data, triggering tailored keyboards and validation.

Pair them with attributes like name (for submission) and id (for labels).

Common Input Types

Choose the right type for semantic accuracy and user experience.



Advanced Inputs: File, Range, Color

Specialized types enhance functionality without plugins.


1. file: <input type="file" accept="image/*"> – Restrict file types.

2. range: <input type="range" min="0" max="100" value="50"> – Sliders.

3. color: <input type="color"> – Native picker.

4. textarea: Multi-line text: <textarea rows="5" cols="30"></textarea>.


Select and Datalist

xml
<select name="country">
<optgroup label="Europe">
<option value="uk">United Kingdom</option>
</select>
<datalist id="suggestions">
<option value="JavaScript">
</datalist>
<input list="datalist">

Form Labels and Accessibility

Labels connect inputs to descriptions, essential for screen readers and click targets.


Proper Label Implementation

Always associate labels with inputs for WCAG compliance.


1. for/id pairing: <label for="email">Email:</label> <input id="email">.

2. Wrapper label: <label>Email: <input></label> – Implicit association.

3. Add aria-describedby for extra help text.


Accessibility Checklist:


1. Every input has a label.

2. Use <label> over placeholders (placeholders vanish on focus).

3. Group with <fieldset>.


Native Validation Attributes

HTML5 validation runs instantly—no JavaScript needed. Browsers show bubbles with custom messages.

Required Fields and Patterns

Force completion and format checking.


Core Attributes:


required: Blocks submit if empty.

minlength/maxlength: Character limits.

pattern: Regex for custom formats (e.g., pattern="[A-Za-z]{3,}").


Phone Example:

xml
<input type="tel" name="phone" pattern="[0-9]{10}" required
title="Enter 10-digit phone number">


Numeric and Value Constraints

Control ranges and steps precisely.


Form Submission and Security

Handle data securely from the start.


GET: Bookmarks/search—data in URL:

The GET method sends data through the URL, making it suitable for search queries and bookmarkable pages. However, since the data is visible in the address bar, it should not be used for sensitive information.


POST: Forms, uploads—secure transmission:

The POST method sends data in the request body instead of the URL, making it more secure for submitting forms and uploading files. It is ideal for handling sensitive or large amounts of data that should not be exposed publicly.


Best Practices:

xml
<form method="POST" autocomplete="off">
<input type="email" name="email" required autocomplete="username">
<input type="password" name="pass" required autocomplete="current-password">
</form>


Practical Form Building Process

Tie it together with a step-by-step workflow.


1. Plan fields: List required data and types.

2. Add semantic structure: Fieldsets, labels.

3. Apply validation: required, pattern, min/max.

4. Test accessibility: Tab through, screen reader check.


Complete Contact Form Example

xml
<form action="/send" method="POST">
<fieldset>
<legend>Contact Info</legend>
<label for="name">Name:
<input id="name" type="text" name="name" required minlength="2">
</label>
<label for="email">Email:
<input id="email" type="email" name="email" required>
</label>
</fieldset>
<label for="message">Message:
<textarea id="message" name="message" required></textarea>
</label>
<button type="submit">Send</button>
</form>