Lesson 2 of 9
Form Validation & Accessibility
Learn HTML5 built-in validation (required, pattern, min/max, minlength/maxlength), custom validation styling with CSS pseudo-classes (:valid, :invalid), and making forms fully accessible with ARIA attributes and proper labeling.
HTML5 Validation Attributes
HTML5 provides powerful validation without JavaScript. The required attribute prevents empty submissions. The pattern attribute accepts a regex (e.g., pattern='[A-Za-z]{3,}' for 3+ letters). min/max constrain numbers and dates. minlength/maxlength limit text length. The browser validates on submit and shows localized error messages. Use CSS :valid and :invalid pseudo-classes to style inputs based on validation state.
Validating input ensures data integrity before submission
Form Accessibility Checklist
- Every input MUST have a <label> with matching for/id - screen readers read the label when input is focused
- Group related inputs with <fieldset> and <legend> - radio buttons, checkboxes, address fields
- Use aria-describedby to link helper text to inputs - describes format requirements or errors
- aria-required='true' supplements the required attribute for older assistive technology
- aria-invalid='true' programmatically marks invalid fields for screen reader announcements
- Never rely on placeholder as the only label - it disappears on focus, failing accessibility guidelines
- Tab order must be logical - use tabindex only when default DOM order is insufficient
Tip
Never use placeholder text as a replacement for <label>. Placeholders disappear when the user starts typing, leaving no indication of what the field is for. Screen readers may not announce placeholder text. Always use a visible <label> with a matching for/id pair - placeholders are supplementary hints, not labels.
Validation & Accessibility Code
<!-- Accessible registration form with validation -->
<form novalidate>
<fieldset>
<legend>Create Account</legend>
<label for="username">Username</label>
<input type="text" id="username" name="username"
required minlength="3" maxlength="20"
pattern="[a-zA-Z0-9_]+"
aria-describedby="username-help">
<small id="username-help">3-20 characters, letters, numbers, underscores only</small>
<label for="email">Email</label>
<input type="email" id="email" name="email" required
aria-describedby="email-help">
<small id="email-help">We will never share your email</small>
<label for="pwd">Password</label>
<input type="password" id="pwd" name="password"
required minlength="8"
pattern="(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{8,}"
aria-describedby="pwd-help">
<small id="pwd-help">Min 8 chars: 1 uppercase, 1 lowercase, 1 number</small>
</fieldset>
<fieldset>
<legend>Preferences</legend>
<label>
<input type="checkbox" name="newsletter" value="yes">
Subscribe to newsletter
</label>
<p>Preferred contact:</p>
<label><input type="radio" name="contact" value="email" checked> Email</label>
<label><input type="radio" name="contact" value="phone"> Phone</label>
</fieldset>
<button type="submit">Create Account</button>
</form>Quick Quiz: Validation & Accessibility
Key Takeaways
- Learn HTML5 built-in validation (required, pattern, min/max, minlength/maxlength), custom validation styling with CSS pseudo-classes (:valid, :invalid), and making forms fully accessible with ARIA attributes and proper labeling.
- Every input MUST have a <label> with matching for/id - screen readers read the label when input is focused
- Group related inputs with <fieldset> and <legend> - radio buttons, checkboxes, address fields
- Use aria-describedby to link helper text to inputs - describes format requirements or errors
Finished this lesson?