📝 Lesson Overview: Building Accessible & Validated Web Forms
HTML forms provide a structured way to collect information from visitors. In this lesson, you will build accessible forms, explore common HTML5 input types, use built-in validation attributes, and test a working registration form.
- Understand the
form,label,input,select, andtextareaelements. - Select appropriate input types for different kinds of data.
- Use
required,minlength,maxlength,min,max, andpattern. - Build a form that is keyboard-friendly and accessible.
- Understand the difference between browser validation and server-side validation.
Prerequisite: Basic HTML document structure, elements, and attributes.
1. Introduction to HTML Forms & GET vs. POST
The
element groups controls that collect information. When submitted, the browser sends the form data according to the form's action and method.
| Attribute | Purpose | Example |
|---|---|---|
action | URL that receives the submitted data. | action="/contact" |
method | HTTP method used to submit data. | method="post" |
name | Key used for a control's submitted value. | name="email" |
autocomplete | Hints whether the browser may autofill a field. | autocomplete="email" |
novalidate | Disables the browser's automatic constraint validation for that form. | |
| GET | POST |
|---|---|
| Form values are usually appended to the URL as a query string. | Form values are sent in the request body. |
| Useful for searches and requests that retrieve data. | Commonly used for creating or changing data. |
| Values may appear in browser history and copied URLs. | Values are not normally displayed in the URL. |
Important: A form control generally needs a
name to be included in submitted form data. Neither GET nor POST automatically encrypts data—always use HTTPS in transit.
HTML5 (form_get_post_examples.html)
Result:
POST sends payload fields in the request body; GET appends query parameters (?q=...) to the action URL.
2. Common HTML5 Input Types & Form Elements
The
type attribute helps the browser present an appropriate control and keyboard, while , , , and handle multi-line text, dropdowns, and grouped choices.
| Type | Use | Example |
|---|---|---|
text | General short text | |
email | Email address format | |
password | Masked password entry | |
tel | Telephone number | |
number | Numeric value | |
date / time | Date picker / time value | |
url | Web address | |
checkbox / radio | Independent toggle / one choice from a group | |
file / range / color | File selection, range slider, color picker | |
HTML5 (input_types_and_controls.html)
Result:
Accessible controls paired with explicit
3. Built-in HTML5 Validation, Accessibility & Client vs. Server
Constraint attributes catch common errors in the browser before submission, while server-side validation enforces rules securely on incoming requests:
- Accessibility & Labels: Connect every control via
for/id, describe formats witharia-describedby, keep focus states visible, and never rely on color alone for errors. - Client-Side vs. Server-Side Validation: Browser validation improves UX quickly, but users can bypass client checks—always re-validate on the server.
| Attribute | What It Checks | Example |
|---|---|---|
required | Value must be provided. | required |
minlength / maxlength | Character length bounds. | minlength="8" maxlength="64" |
min / max / step | Numeric or date bounds and increments. | min="18" max="100" |
pattern | Matches a regular expression. | pattern="[A-Za-z]{3,}" |
Best practice: Pair client-side validation for helpful instant feedback with server-side validation for every untrusted field before saving or processing data.
HTML5 (accessible_validation_field.html)
Include your country code, if applicable.
Result:
Field linked to help text via aria-describedby with autocomplete="tel" for smooth autofill.
4. Interactive Registration Form, Knowledge Check & Checklist
Test the live registration form below (client-side validation demo only; no data is sent to a server), try the 3-question check, and review the checklist and assignment:
Knowledge Check
1. Which attribute specifies the key used when a control's value is submitted?
2. Which input type provides built-in email-format checking?
3. Is client-side validation enough to secure submitted data?
Lesson Checklist & Practice Assignment
- Connect every label to its control (
for/id), choose accurate HTML5 input types, and group related controls withfieldsetandlegend. - Use
required,minlength,maxlength,min,max, andpatternin the browser while always re-validating on the server. - Assignment: Build a student admission form with name, email, phone, date of birth, course, and address fields; apply constraints and accessible labels; and test invalid inputs and keyboard-only navigation.
JavaScript (form_validation_snippet.js)
const form = document.querySelector('#registrationForm');
form.addEventListener('submit', (event) => {
event.preventDefault();
if (!form.checkValidity()) {
form.reportValidity();
return;
}
document.querySelector('#formResult').textContent =
'Form looks valid. Demo only — no data was sent.';
});
Result:
Uses checkValidity() and reportValidity() to trigger native browser validation messages cleanly.