Components

Error message

Follow the validation pattern and show an error message when there is a validation error. In the error message explain what went wrong and how to fix it.

<div class="govuk-form-group govuk-form-group--error">
  <fieldset class="govuk-fieldset" role="group" aria-describedby="passport-issued-hint passport-issued-error">
    <legend class="govuk-fieldset__legend govuk-fieldset__legend--l">
      <h1 class="govuk-fieldset__heading">
        When was your passport issued?
      </h1>
    </legend>
    <div id="passport-issued-hint" class="govuk-hint">
      For example, 12 11 2007
    </div>
    <span id="passport-issued-error" class="govuk-error-message">
      <span class="govuk-visually-hidden">Error:</span> The date your passport was issued must be in the past
    </span>
    <div class="govuk-date-input" id="passport-issued">
      <div class="govuk-date-input__item">
        <div class="govuk-form-group">
          <label class="govuk-label govuk-date-input__label" for="passport-issued-day">
            Day
          </label>
          <input class="govuk-input govuk-date-input__input govuk-input--width-2 govuk-input--error" id="passport-issued-day" name="passport-issued-day" type="text" value="6" pattern="[0-9]*" inputmode="numeric"></div>
      </div>
      <div class="govuk-date-input__item">
        <div class="govuk-form-group">
          <label class="govuk-label govuk-date-input__label" for="passport-issued-month">
            Month
          </label>
          <input class="govuk-input govuk-date-input__input govuk-input--width-2 govuk-input--error" id="passport-issued-month" name="passport-issued-month" type="text" value="3" pattern="[0-9]*" inputmode="numeric"></div>
      </div>
      <div class="govuk-date-input__item">
        <div class="govuk-form-group">
          <label class="govuk-label govuk-date-input__label" for="passport-issued-year">
            Year
          </label>
          <input class="govuk-input govuk-date-input__input govuk-input--width-4 govuk-input--error" id="passport-issued-year" name="passport-issued-year" type="text" value="2076" pattern="[0-9]*" inputmode="numeric"></div>
      </div>
    </div>

  </fieldset>
</div>
Nunjucks macro options

Use options to customise the appearance, content and behaviour of a component when using a macro, for example, changing the text.

Some options are required for the macro to work; these are marked as "Required" in the option description.

If you're using Nunjucks macros in production with "html" options, or ones ending with "html", you must sanitise the HTML to protect against cross-site scripting exploits.

Primary options
Name Type Description
text string Required. If html is set, this is not required. Text to use within the error message. If html is provided, the text argument will be ignored.
html string Required. If text is set, this is not required. HTML to use within the error message. If html is provided, the text argument will be ignored.
id string Id attribute to add to the error message span tag.
classes string Classes to add to the error message span tag.
attributes object HTML attributes (for example data attributes) to add to the error message span tag
visuallyHiddenText string A visually hidden prefix used before the error message. Defaults to "Error".
{% from "govuk/components/date-input/macro.njk" import govukDateInput %}

{{ govukDateInput({
  fieldset: {
    legend: {
      text: "When was your passport issued?",
      isPageHeading: true,
      classes: "govuk-fieldset__legend--l"
    }
  },
  hint: {
    text: "For example, 12 11 2007"
  },
  errorMessage: {
    text: "The date your passport was issued must be in the past"
  },
  id: "passport-issued",
  namePrefix: "passport-issued",
  items: [
    {
      classes: "govuk-input--width-2 govuk-input--error",
      name: "day",
      value: "6"
    },
    {
      classes: "govuk-input--width-2 govuk-input--error",
      name: "month",
      value: "3"
    },
    {
      classes: "govuk-input--width-4 govuk-input--error",
      name: "year",
      value: "2076"
    }
  ]
}) }}

When to use this component

Show an error message next to the field and in the error summary when there is a validation error.

Use standard messages for different components.

When not to use this component

Do not use error messages to tell users that they are not eligible or do not have permission to do something. Instead, take them to a page which tells them they’re not eligible and gives them useful information about what to do next.

There are separate patterns for:

How it works

For each error:

  • put the message in red after the question text and hint text
  • use a red border to visually connect the message and the question it belongs to
  • if the error relates to a specific field within the question, give it a red border and refer to that field in the error message - for example: “you must enter a year”

To help screen reader users, the error message component includes a hidden ‘Error:’ before the error message. These users will hear, for example, “Error: The date your passport was issued must be in the past”.

If your error message is written in another language, you can change the prefix as needed, as shown in this example.

<span class="govuk-error-message">
  <span class="govuk-visually-hidden">Gwall:</span> Rhowch eich enw llawn
</span>
Nunjucks macro options

Use options to customise the appearance, content and behaviour of a component when using a macro, for example, changing the text.

Some options are required for the macro to work; these are marked as "Required" in the option description.

If you're using Nunjucks macros in production with "html" options, or ones ending with "html", you must sanitise the HTML to protect against cross-site scripting exploits.

Primary options
Name Type Description
text string Required. If html is set, this is not required. Text to use within the error message. If html is provided, the text argument will be ignored.
html string Required. If text is set, this is not required. HTML to use within the error message. If html is provided, the text argument will be ignored.
id string Id attribute to add to the error message span tag.
classes string Classes to add to the error message span tag.
attributes object HTML attributes (for example data attributes) to add to the error message span tag
visuallyHiddenText string A visually hidden prefix used before the error message. Defaults to "Error".
{% from "govuk/components/error-message/macro.njk" import govukErrorMessage %}

{{ govukErrorMessage({
  text: "Rhowch eich enw llawn",
  visuallyHiddenText: "Gwall"
}) }}

Summarise all errors at the top of the page the user is on using an error summary.

There are 2 ways to use the error message component. You can use HTML or, if you are using Nunjucks or the GOV.UK Prototype Kit, you can use the Nunjucks macro.

Legend

<div class="govuk-form-group govuk-form-group--error">
  <fieldset class="govuk-fieldset" aria-describedby="nationality-hint nationality-error">
    <legend class="govuk-fieldset__legend govuk-fieldset__legend--l">
      <h1 class="govuk-fieldset__heading">
        What is your nationality?
      </h1>
    </legend>
    <div id="nationality-hint" class="govuk-hint">
      If you have dual nationality, select all options that are relevant to you.
    </div>
    <span id="nationality-error" class="govuk-error-message">
      <span class="govuk-visually-hidden">Error:</span> Select if you are British, Irish or a citizen of a different country
    </span>
    <div class="govuk-checkboxes">
      <div class="govuk-checkboxes__item">
        <input class="govuk-checkboxes__input" id="nationality" name="nationality" type="checkbox" value="british">
        <label class="govuk-label govuk-checkboxes__label" for="nationality">
          British
        </label>
      </div>
      <div class="govuk-checkboxes__item">
        <input class="govuk-checkboxes__input" id="nationality-2" name="nationality" type="checkbox" value="irish">
        <label class="govuk-label govuk-checkboxes__label" for="nationality-2">
          Irish
        </label>
      </div>
      <div class="govuk-checkboxes__item">
        <input class="govuk-checkboxes__input" id="nationality-3" name="nationality" type="checkbox" value="other">
        <label class="govuk-label govuk-checkboxes__label" for="nationality-3">
          Citizen of another country
        </label>
      </div>
    </div>

  </fieldset>
</div>
Nunjucks macro options

Use options to customise the appearance, content and behaviour of a component when using a macro, for example, changing the text.

Some options are required for the macro to work; these are marked as "Required" in the option description.

If you're using Nunjucks macros in production with "html" options, or ones ending with "html", you must sanitise the HTML to protect against cross-site scripting exploits.

Primary options
Name Type Description
text string Required. If html is set, this is not required. Text to use within the error message. If html is provided, the text argument will be ignored.
html string Required. If text is set, this is not required. HTML to use within the error message. If html is provided, the text argument will be ignored.
id string Id attribute to add to the error message span tag.
classes string Classes to add to the error message span tag.
attributes object HTML attributes (for example data attributes) to add to the error message span tag
visuallyHiddenText string A visually hidden prefix used before the error message. Defaults to "Error".
{% from "govuk/components/checkboxes/macro.njk" import govukCheckboxes %}

{{ govukCheckboxes({
  idPrefix: "nationality",
  name: "nationality",
  fieldset: {
    legend: {
      text: "What is your nationality?",
      isPageHeading: true,
      classes: "govuk-fieldset__legend--l"
    }
  },
  hint: {
    text: "If you have dual nationality, select all options that are relevant to you."
  },
  errorMessage: {
    text: "Select if you are British, Irish or a citizen of a different country"
  },
  items: [
    {
      value: "british",
      text: "British"
    },
    {
      value: "irish",
      text: "Irish"
    },
    {
      value: "other",
      text: "Citizen of another country"
    }
  ]
}) }}

Label

<div class="govuk-form-group govuk-form-group--error">
  <label class="govuk-label" for="national-insurance-number">
    National Insurance number
  </label>
  <div id="national-insurance-number-hint" class="govuk-hint">
    It’s on your National Insurance card, benefit letter, payslip or P60. For example, ‘QQ 12 34 56 C’.
  </div>
  <span id="national-insurance-number-error" class="govuk-error-message">
    <span class="govuk-visually-hidden">Error:</span> Enter a National Insurance number in the correct format
  </span>
  <input class="govuk-input govuk-input--error" id="national-insurance-number" name="national-insurance-number" type="text" aria-describedby="national-insurance-number-hint national-insurance-number-error">
</div>
Nunjucks macro options

Use options to customise the appearance, content and behaviour of a component when using a macro, for example, changing the text.

Some options are required for the macro to work; these are marked as "Required" in the option description.

If you're using Nunjucks macros in production with "html" options, or ones ending with "html", you must sanitise the HTML to protect against cross-site scripting exploits.

Primary options
Name Type Description
text string Required. If html is set, this is not required. Text to use within the error message. If html is provided, the text argument will be ignored.
html string Required. If text is set, this is not required. HTML to use within the error message. If html is provided, the text argument will be ignored.
id string Id attribute to add to the error message span tag.
classes string Classes to add to the error message span tag.
attributes object HTML attributes (for example data attributes) to add to the error message span tag
visuallyHiddenText string A visually hidden prefix used before the error message. Defaults to "Error".
{% from "govuk/components/input/macro.njk" import govukInput %}

{{ govukInput({
  label: {
    text: "National Insurance number"
  },
  hint: {
    text: "It’s on your National Insurance card, benefit letter, payslip or P60. For example, ‘QQ 12 34 56 C’."
  },
  id: "national-insurance-number",
  name: "national-insurance-number",
  errorMessage: {
    text: "Enter a National Insurance number in the correct format"
  }
}) }}

Be clear and concise

Describe what has happened and tell them how to fix it. The message must be in plain English, use positive language and get to the point.

Do not use:

  • technical jargon like ‘form post error’, ‘unspecified error’ and ‘error 0x0000000643’
  • words like ‘forbidden’, ‘illegal’, ‘you forgot’ and ‘prohibited’
  • ‘please’ because it implies a choice
  • ‘sorry’ because it does not help fix the problem
  • ‘valid’ and ‘invalid’ because they do not add anything to the message
  • humourous, informal language like ‘oops’

Do not give an example in the error message if there is an example on the screen. For example, if you are asking for a National Insurance number and include ‘QQ 12 34 56 C’ as hint text, do not include an example in the error message.

Above all, aim for clarity.

Read the message out loud to see if it sounds like something you would say.

Be consistent

Use the same message next to the field and in the error summary so they:

  • look, sound and mean the same
  • make sense out of context
  • reduce the cognitive effort needed to understand what has happened

Use the question or form label in the error to provide context. For example, ‘Enter how many hours you work a week’ for ‘How many hours do you work a week?’

Be specific

General errors are not helpful to everyone. They do not make sense out of context. Avoid messages like:

  • ‘An error occurred’
  • ‘Answer the question’
  • ‘Select an option’
  • ‘Fill in the field’
  • ‘This field is required’

Different errors need different messages. For example, text fields may be:

  • empty
  • too long
  • too short
  • using characters that are not allowed
  • in the wrong format

An error for a specific situation is more helpful. It will tell someone what has happened and how to fix it.

Use instructions and descriptions

Some errors work better as instructions and some work better as descriptions. For example:

  • ‘Enter your first name’ is clearer, more direct and natural than ‘First name must have an entry’
  • ‘Enter a first name that is 35 characters or fewer’ is wordier, less direct and natural than ‘First name must be 35 characters or fewer’
  • ‘Enter a date after 31 August 2017 for when you started the course’ is wordier, less direct and natural than ‘Date you started the course must be after 31 August 2017’

Use both instructions and descriptions, but use them consistently. For example, use an instruction for empty fields like ‘Enter your name’, but a description like ‘Name must be 35 characters or fewer’ for entries that are too long.

Use error message templates

Use template messages for common errors on:

Track errors

Find out how often people see them. This will let you:

  • improve content
  • A/B test variations
  • redesign a journey

Research on this component

Error messages designed using this guidance have been tested with all types of users in live services, including tax credits.

Research showed users:

  • understood what went wrong
  • knew how to fix the problem
  • were able to recover from the error

If you’ve used this component, get in touch to share your user research findings.

Help improve this page

To help make sure that this page is useful, relevant and up to date, you can:

Need help?

If you’ve got a question about the GOV.UK Design System, contact the team.