# Forms Specialist reference: Required Fields Part of the `forms-specialist` skill. Read this only when the task reaches these sections. ## Required Fields ```html ``` Requirements: - Use the native `required` attribute -- it gives browser validation and screen reader announcement for free - Add `aria-required="true"` for reinforcement (some screen readers prefer it) - If using an asterisk, hide it from screen readers with `aria-hidden="true"` -- the `required` attribute already announces "required" - Explain the asterisk convention at the top of the form: "Fields marked with * are required" - Never indicate required status through color alone ## Grouping with Fieldset and Legend Related inputs MUST be grouped: ```html
Shipping Address
``` When to use fieldset/legend: - Radio button groups (always) - Checkbox groups (always) - Related field groups (address, payment info, personal details) - When the group label provides essential context for understanding individual fields ```html
Preferred contact method
Notification preferences
``` Without fieldset/legend, a screen reader user hearing "Email" has no idea it refers to a contact method preference. ## Error Handling This is the most commonly broken part of form accessibility. ### Error Message Structure ```html ``` Requirements: - `aria-invalid="true"` on the field with an error - Error message linked via `aria-describedby` - Error text is visible (not just an icon or color change) - Error text is specific: "Please enter a valid email address" not "Invalid input" - Remove `aria-invalid` when the error is corrected ### Error Summary on Submit For forms with multiple errors, provide a summary at the top: ```html ``` Requirements: - `role="alert"` so screen readers announce it immediately - `tabindex="-1"` so focus can be moved there programmatically - Focus moves to the error summary on submit - Each error links to the offending field - Heading describes the count of errors ### Focus Management on Error ```javascript // On form submit with errors: const errorSummary = document.getElementById('error-summary'); errorSummary.focus(); // Focus the summary // If no summary, focus the first invalid field: const firstError = document.querySelector('[aria-invalid="true"]'); firstError.focus(); ``` ### Inline Validation If validating as the user types or on blur: - Do not validate on every keystroke -- wait for blur or a pause - Announce errors via `aria-live="polite"` or `aria-describedby` association - Remove errors immediately when corrected - Never block input while validating ### Error Indicators - Red border alone is NOT sufficient - Must include visible error text - Should include an icon for additional visual indicator - Associate the error icon with `aria-hidden="true"` (the text conveys the message) ```html ``` ## Autocomplete Use `autocomplete` attributes to help browsers and password managers fill fields: ```html ``` This is a WCAG 1.3.5 requirement (Input Purpose). It helps users with cognitive disabilities by enabling autofill and helps password managers work correctly.