The role of an error message
NN/g defines an error message as the system interrupting the user's workflow to report a condition that is incomplete, incompatible, or undesirable according to how the system operates. NN/g also calls for the message to explain the problem clearly and offer the user possible ways to correct it.
The GOV.UK Design System says that a validation error message should explain what went wrong and how to fix it. A message next to a form field and an Error summary at the top of the page address the same error in two locations. The message next to the field appears with the relevant question; the Error summary brings together the errors on that page.
Describe the problem in text
According to W3C's WCAG Error Identification success criterion, when an input error is detected automatically, the item in error must be identified and the error described to the user in text. Redisplaying a form without any explanation after a failed submission is not enough. The user must be told the nature of the error and which item it concerns.
W3C defines an input error as information provided by the user that is not accepted. This includes information the page requires but the user has not entered. Information entered in a format or outside the values allowed is also an input error. For example, entering letters in a telephone field that accepts only numbers is one of the examples W3C gives.
If a value is outside the allowed range and the page automatically changes it to a value within that range, the need to explain the error remains. According to W3C, an explanation informing the user of the changed value can satisfy both the Error Identification and Error Suggestion criteria.
NN/g recommends using short, clear text that explains exactly what the problem is instead of a generic error statement. Too much technical detail, however, can make the message harder to understand. The user's understanding of how the system works may not match the way the system is coded.
Specify how to correct it
The WCAG Error Suggestion criterion requires that correction suggestions be provided to the user when they are known for an automatically detected input error. There is a limit to this requirement: it does not apply if providing the suggestions would jeopardize the security or purpose of the content.
In W3C's example, a field asks for the name of a month and the user enters a number. A correction suggestion could be a list of accepted month names. The system could also interpret the number entered as a different month format and ask which month was intended. In both cases, the suggestion relates to the values the field accepts.
W3C treats a field requiring a specific format and a field requiring a limited set of values as separate cases. Error descriptions and suggested correction text are among the techniques that can be used in these cases. A field can also be both required and require information in a specific format.
NN/g also recommends offering possible solutions instead of only stating the problem. In its example, a message about an out-of-stock product could include information about when the product will be available again or an option to receive a stock notification. NN/g also describes linking from a short message to more detailed information when further explanation is needed.
Place the message near the relevant field
NN/g recommends showing the error indicator close to the interface element where the error occurred. The proximity of the message to the relevant element helps the user associate the explanation with the element that needs attention. In the GOV.UK Design System, a validation error is shown both next to the field and in the Error summary.
In the GOV.UK Design System layout, the error message appears in red after the question and any hint text. A red border visually connects the message and the question it concerns. If the error relates to a specific field within the question, that field also receives a red border and the message refers to it.
The GOV.UK Design System says that the message next to the field and the message in the Error summary should be the same. It also recommends that the error message directly include the wording from the question or fieldset label. In one of its examples, the same field name is used in both the label asking for an address line and the error message.
WCAG Error Identification does not require a single location for displaying an error. W3C gives examples of listing errors at the beginning of a form, showing them next to the relevant fields, or presenting them in an alert or dialog. The criterion requires the error to be presented to the user in text or as a text alternative.