A red border says "something is wrong" to some people. What about people who cannot see colour, or who use a screen reader?
Keep the question in mind. By the end of the lesson you will be able to answer it, and prove it with working code.
02Start with working code
The message is real text, linked to the field with aria-describedby, and the field is marked aria-invalid. Screen readers read the message when the field is focused.
const email = document.querySelector("#email");
const error = document.querySelector("#email-error");
email.setAttribute("aria-invalid", "true");
error.textContent = "Enter an email like name@example.com";
console.log(email.getAttribute("aria-describedby"), error.textContent);
03Objective and prerequisites
Learning objective: Show validation feedback that everyone can perceive: visible text messages linked with aria-describedby, aria-invalid, focus management and live regions.
Every field needs a visible label (<label for="id">). Placeholders disappear when you type and are not labels.
Error messages must be text, not only colour. Put the message in an element near the field.
Link the message to the field with aria-describedby="message-id" and set aria-invalid="true" on the field, so screen readers announce both.
After a failed submit, move focus to the first invalid field (field.focus()) so keyboard and screen reader users land where the problem is.
Use a live region (role="status" or aria-live="polite") for messages that appear without a page change, such as "Saved" or an error count. role="alert" interrupts, so reserve it for urgent errors.
Live regions should exist in the page before their text changes; inserting a new region with text already inside is often not announced.
An error summary at the top of a long form, with links to each field, helps everyone; set focus on the summary after submission.
Remove aria-invalid and clear messages as soon as the value becomes valid, and avoid announcing errors on every keystroke while someone is still typing.
05Definitions
aria-describedby
An attribute that links an element to the ids of elements that describe it, such as an error message.
aria-invalid
An attribute telling assistive technology that a field's value is invalid.
Live region
An area marked with aria-live or role="status" whose text changes are announced by screen readers.
Focus management
Moving keyboard focus deliberately, for example to the first invalid field.
Error summary
A list of all errors at the top of a form, usually with links to each field.
Accessible interaction laboratory. Submit a form with errors and see which attributes, messages, live announcements and focus changes a screen reader user would get.
Three exercises: explore and modify, complete the implementation, then build independently. Tests check behaviour, so any correct solution passes.
A field without a label
Connect a label to its field.
const field = document.querySelector("#postcode");
console.log(field.labels.length);
Accessible error helper
Keep text, aria-invalid and aria-describedby in sync.
function setError(field, message) {
// TODO: put message in the element with id field.id + "-error",
// set aria-describedby to that id, and set aria-invalid="true"
// when there is a message or remove aria-invalid when there is not
}
const city = document.querySelector("#city");
setError(city, "Enter a city");
console.log(city.getAttribute("aria-invalid"), city.getAttribute("aria-describedby"), document.querySelector("#city-error").textContent);
Accessible form submission
Combine validation, messages, focus and a live status.
// On submit (preventDefault): for each named field, set its "<id>-error"
// text ("Required" for valueMissing, "Invalid" for any other problem, ""
// if valid) and aria-invalid; focus the first invalid field; set #status
// to "N error(s)" or "Thanks!".
// Then fill only the name with "Ana" and submit.
09Prediction
Commit to an answer first, then run the real code to compare.
Government services are required to meet accessibility standards. Their forms show an error summary with links, inline messages connected by aria-describedby, and move focus to the summary after submission, so every visitor can complete them with a keyboard or screen reader.
1. Why is a red border alone not enough to show an error?
Borders are slow
Some people cannot perceive colour and screen readers do not announce it
Red is not allowed
Browsers remove borders
2. What does aria-describedby do?
Hides an element
Links a field to the element(s) that describe it
Validates a field
Sets a placeholder
3. Where should focus go after a failed submit?
Stay on the button
The first invalid field or an error summary
The page title
The browser address bar
4. Why is the Saved message not announced by screen readers?
const p = document.createElement("p");
p.setAttribute("role", "status");
p.textContent = "Saved";
document.body.append(p);
The live region is created at the same moment as its text
textContent is wrong
status must be a div
Messages cannot be announced
5. Is a placeholder a substitute for a label?
Yes
No, it disappears when typing and is not reliably announced
Only for emails
Only on mobile
6. Which is better for announcing an urgent payment failure?
role="alert"
A console.log
A colour change
A title change
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
A field without a label: Prints 1; The label is connected
Accessible error helper: Shows the error; Clears the error
Accessible form submission: Reports one error and focuses email; Email error says Required; Valid form says Thanks!
The lesson lives in the companion repository at curriculum/level-03-interactive-web-applications/module-13-forms-and-validation/lesson-065-accessible-form-feedback/. Every example on this page is generated from those files.
These links open the files in the GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Engineering-Academy). You can also use the ZIP download above.
15Summary and next steps
Errors must be text, not only colour.
Link messages with aria-describedby and mark fields with aria-invalid.
Move focus to the first problem or an error summary.
Use live regions that already exist for dynamic messages.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationaccessible form feedback
Context: I am learning accessible form feedback in JavaScript.
Task: Explain labels, aria-describedby, aria-invalid, focus management and live regions for form errors.
Constraints: Beginner-friendly language, short runnable examples, no libraries.
Expected behaviour: I can explain the idea in my own words and predict small programs.
Testing: Finish with three quick questions for me to answer.
Code predictionaccessible form feedback drill
Context: I have practised accessible form feedback.
Task: Give me six small form snippets and ask what a screen reader user would hear or where focus ends up.
Constraints: Wait for my answer before revealing each result.
Expected behaviour: Each question has exactly one correct output.
Testing: Explain any I miss with a step-by-step trace.
Debuggingaccessible form feedback bug
Context: Screen reader users say they cannot tell which field is wrong.
Task: Help me find the cause with questions instead of giving me the fix.
Constraints: Do not rewrite my program.
Expected behaviour: I locate and fix the bug myself.
Testing: Suggest the console.log lines that would reveal the problem.
Exercise generationaccessible form feedback practice
Context: I finished the accessible form feedback exercises.
Task: Create three exercises that add accessible feedback to a form.
Constraints: Fictional data and exact expected output.
Expected behaviour: Each exercise has tests I can run.
Testing: Put the solutions in a separate hidden section.
Code reviewaccessible form feedback review
Context: Here is my code that uses accessible form feedback.
Task: Review my form for missing labels, colour-only errors and lost focus.
Constraints: Explain each suggestion briefly and keep behaviour identical.
Expected behaviour: The code is clearer and has no hidden mutation or edge-case bug.
Testing: List the changes in order of importance.