On this page
Accessible Forms & Validation
Prev: Semantic Document Structure | Up: tutorials | Next: Media & Responsive Images
Rel: Forms & Inputs, Form & Validation Attributes, Pseudo-Classes, Forms, Validation & Fetch
Ref: MDN Web Docs — HTML Forms
Tags: #public #web #html #tutorials #forms
Contents
Connecting Labels, Hints & Errors
Bind <label> to its input via for/id, link helper or error text via aria-describedby, and use aria-invalid="true" when an error is shown.
<form method="post">
<div>
<label for="handle">Username</label>
<input
id="handle"
name="handle"
type="text"
required
minlength="4"
pattern="[a-z0-9_]+"
aria-describedby="handle-hint"
/>
<p id="handle-hint">Use at least 4 lowercase letters, digits, or underscores.</p>
</div>
<button type="submit">Save Profile</button>
</form>
Styling Validation States with CSS
Prefer :user-valid and :user-invalid over :valid/:invalid so fields are not styled as errors before the user interacts with them:
input:user-invalid {
border-color: #c62828;
}
input:user-valid {
border-color: #2e7d32;
}
Linked from
- tuts/css/selectors/pseudo_classes
- tuts/html/attributes/form_and_validation_attributes
- tuts/html/elements/forms_and_inputs
- tuts/html/tutorials
- tuts/html/tutorials/accessible_forms_and_validation
- tuts/html/tutorials/media_and_responsive_images
- tuts/html/tutorials/semantic_document_structure
- tuts/web/tutorials/forms_validation_and_fetch
Related notes
- tuts/html/attributes/form_and_validation_attributes Linked here; links to this page; shared tags: #forms, #html, #web
- tuts/html/elements/forms_and_inputs Linked here; links to this page; shared tags: #forms, #html, #web
- tuts/html/tutorials Linked here; links to this page; shared tags: #html, #tutorials, #web
- tuts/html/tutorials/media_and_responsive_images Linked here; links to this page; shared tags: #html, #tutorials, #web
- tuts/html/tutorials/semantic_document_structure Linked here; links to this page; shared tags: #html, #tutorials, #web
- tuts/web/tutorials/forms_validation_and_fetch Linked here; links to this page; shared tags: #forms, #tutorials, #web
- tuts/css/selectors/pseudo_classes Linked here; links to this page; shared tags: #web
- tuts/html/elements/forms shared tags: #forms, #html, #web
- tuts/html/elements/forms/button shared tags: #forms, #html, #web
- tuts/html/elements/forms/datalist shared tags: #forms, #html, #web