1.2: HTML5 FORMS & VALIDATION — INPUTS, CONSTRAINTS & ACCESSIBILITY

Collect user input, choose the right form controls, and validate data with HTML5 attributes—with an interactive registration form, knowledge check, and copyable code examples.

📝 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, and textarea elements.
  • Select appropriate input types for different kinds of data.
  • Use required, minlength, maxlength, min, max, and pattern.
  • 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
actionURL that receives the submitted data.action="/contact"
methodHTTP method used to submit data.method="post"
nameKey used for a control's submitted value.name="email"
autocompleteHints whether the browser may autofill a field.autocomplete="email"
novalidateDisables 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
Preferred learning mode
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 with aria-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
requiredValue must be provided.required
minlength / maxlengthCharacter length bounds.minlength="8" maxlength="64"
min / max / stepNumeric or date bounds and increments.min="18" max="100"
patternMatches 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:
Enter at least 2 characters.
Use at least 8 characters for this exercise.

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 with fieldset and legend.
  • Use required, minlength, maxlength, min, max, and pattern in 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.