eBay MIND Patterns
  • Introduction
  • Messaging
    • Alert Dialog
    • Confirm Dialog
    • File Preview Card
    • Form Validation
    • Inline Notice
    • Input Meter
    • Input Validation
    • Page Notice
    • Star Rating (static)
    • Time
    • Toast Dialog
    • Tourtip
  • Input
    • Button
    • Checkbox
    • Chips Combobox
    • Combobox
    • Date Picker
    • File Input
    • Input Dialog
    • Listbox
    • Listbox Button
    • Menu
    • Menu Button
    • Phone Input
    • Radio
    • Select
    • Star Rating (interactive)
    • Switch
    • Toggle Button
    • Toggle Button Group
  • Navigation
    • Breadcrumbs
    • Fake Menu Button
    • Fake Tabs
    • Link
    • Pagination
    • Skip Navigation
    • Tile
  • Disclosure
    • Accordion
    • Carousel
    • Lightbox Dialog
    • Details
    • Flyout
    • Footnote
    • Infotip Button
    • Panel Dialog
    • Pulldown List
    • Segmented Buttons
    • Tabs
    • Tooltip
  • Structure
    • Description List
    • Form
    • Heading
    • Image
    • Item Tile
    • Layout Grid
    • Region
    • Table
    • Table Cell
  • Techniques
    • Active Descendant
    • Ambiguous Label
    • Background Icon
    • Keyboard Trap
    • Live Region
    • Offscreen Text
    • Roving Tabindex
    • Skip to Main Content
    • Alternative Text
  • Anti-Patterns
    • Disabling Pinch-to-Zoom
    • Hand Cursor on Buttons
    • JavaScript HREF
    • Layout Table
    • Mouse Hover on Static Elements
    • Open New Window
    • Setting Focus on Page Load
    • Tabindex-itis
    • Title Tooltip
  • Appendix
    • ARIA Essentials
    • Checklist
    • FAQ
    • Keyboard Interface
    • Known Issues
    • Legacy Patterns
      • Fullscreen Dialog
    • MIND Pattern Template
    • Pattern Naming Scheme
    • References
    • Utilities
Powered by GitBook
On this page
  • Introduction
  • Working Examples
  • Terminology
  • Best Practices
  • Interaction Design
  • Developer Guide
  • ARIA Reference
  • Related Articles
  1. Messaging

Form Validation

Notification of invalid input after user has attempted to submit form.

PreviousFile Preview CardNextInline Notice

Last updated 4 months ago

Introduction

Form validation notifies users of invalid and missing data after a user has attempted to submit the form.

After attempting to submit a form, the validation results can either be rendered immediately on the client, or rendered after a full page reload.

Working Examples

Terminology

Form Validation: The pattern as a whole, containing the following sub parts.

Notice landmark: page notice or section notice containing list of error links

Error link: internal link that moves focus to invalid input.

Invalid input: a form control with an invalid value

Inline notice: displayed next to an invalid input

Best Practices

Immediate client-side results must be rendered using a section notice directly above the form.

The notice must contain a list of errors that link directly to their respective invalid inputs.

The errors must be visible to all users. Do not try and hide the errors from sighted users!

Interaction Design

This section provides interaction design for keyboard, screen reader and pointing devices.

Keyboard

The notice landmark must receive programmatic focus when rendered.

The notice landmark should not be in tab order (i.e it should have tabindex="-1").

Activating an error link must move focus to the invalid input.

Inline errors must not be focusable (they are static text).

Screen Reader

User must receive notification of all errors preventing progress.

An invalid input must notify user of the invalid state and the error description.

Notice landmark must be listed as custom landmark and labelled appropriately.

Pointer

Awaiting content for this section.

Developer Guide

The three layers are:

  1. Content (HTML)

  2. Presentation (CSS)

  3. Behaviour (JS)

Content (HTML)

We should never depend solely on client-side JavaScript to validate an important form. Our baseline functionality must be to submit the form to the server for processing, which will display any results after a full page reload.

We will be assuming that the following form has been submitted by the user:

<form action="/processform">
    <span>
        <label for="age">Age</label>
        <input type="text" name="age" id="age" value="foo" />
    </span>
    <span>
        <label for="size">Shoe Size</label>
        <input type="text" name="size" id="size" value="bar" />
    </span>
</form>

Full Page Reload

<section aria-labelledby="page-notice-title" class="page-notice page-notice--attention" role="region">
    <div class="page-notice__header">
        <svg focusable="false" height="24" width="24" role="img" aria-label="Attention">
            <use xlink:href="#icon-attention"></use>
        </svg>
    </div>
    <div class="page-notice__main">
        <h2 class="page-notice__title" id="page-notice-title">Error!</h2>
    </div>
</section>

Using the region role and aria-label ensures that the page notice is listed as a custom "Priority" landmark in assistive technology.

This is one of the few occasions where it is appropriate to create a custom landmark on eBay.

Error Links

It's not enough to just say "Error!" of course! The page notice must also contain descriptive error links to all invalid inputs.

<section aria-labelledby="page-notice-title" class="page-notice page-notice--attention" role="region">
    <div class="page-notice__header">
        <svg focusable="false" height="24" width="24" role="img" aria-label="Attention">
            <use xlink:href="#icon-attention"></use>
        </svg>
    </div>
    <div class="page-notice__main">
        <h2 class="page-notice__title" id="page-notice-title">Error! Please correct the following fields:</h2>
        <ol>
            <li>
                <a href="#age">Age - please enter a valid age (for example, 35)</a>
            </li>
            <li>
                <a href="#size">Shoe-size - please enter a valid shoe size (for example, 8.5)</a>
            </li>
        </ol>
    </div>
</section>

Note that Safari has an issue with setting focus on the input after clicking an internal link (it seems to set focus, but typing is not possible), and so some additional JavaScript is needed to set focus properly.

Invalid Inputs

Use aria-invalid="true" to mark inputs as invalid to assistive technology.

<span>
    <label for="age">Age</label>
    <input aria-describedby="age-description" aria-invalid="true" id="age" name="age" type="text" />
</span>

Inline Notices

<span>
    <label for="age">Age</label>
    <input aria-describedby="age-description" aria-invalid="true" id="age" name="age" type="text" />
    <span id="age-description">Please enter a correct age</span>
</span>

Presentation (CSS)

There is not much to say in terms of CSS, other than the page notice and all errors must be fully visible while the form remains in an invalid state.

Also, we can leverage ARIA states in our CSS.

input[type=text][aria-invalid=true] {
    border: 1px solid red;
}

The aria-invalid state styles invalid fields with a red border. No need to create a class!

Behaviour (JS)

JavaScript enhances our baseline experience by doing everything mentioned above, but all without a full page reload.

We create the exact same notice as in our baseline experience, only now we render the HTML immediately on the client.

<section aria-labelledby="page-notice-title" class="page-notice page-notice--attention" role="region" tabindex="-1">
    <div class="page-notice__header">
        <svg focusable="false" height="24" width="24" role="img" aria-label="Attention">
            <use xlink:href="#icon-attention"></use>
        </svg>
    </div>
    <div class="page-notice__main">
        <h2 class="page-notice__title" id="page-notice-title">Error! Please correct the following fields:</h2>
        <ol>
            <li>
                <a href="#age">Age - please enter a valid age (for example, 35)</a>
            </li>
            <li>
                <a href="#size">Shoe-size - please enter a valid shoe size (for example, 8.5)</a>
            </li>
        </ol>
    </div>
</section>

With JavaScript available, we can also set focus directly on the page notice. This use of focus management is a crucial enhancement for screen reader users.

ARIA Reference

This section gives an overview of our use of ARIA in this pattern.

aria-required

Applied to the input element to denote a required field.

We can also use this attribute as a CSS hook.

aria-invalid

Applied to the input element to denote an invalid field.

We can also use this attribute as a CSS hook.

aria-describedby

Applied to the input element to denote the element containing the error description.

Related Articles

To validate a field before the user attempts to submit the form, please view the pattern.

Experience the pattern in action on our companion .

A full-page reload must render a at the top of the page.

We highly recommend reading by the Norman Nielson Group.

Our implementation follows the strategy. We build in a layered fashion that allows everyone to access the basic content and functionality of a web page.

We can use a to display a prominent error message at the top of the page:

In addition to the main notice, each invalid input must display an :

(Norman Nielson Group)

input validation
eBay MIND Patterns examples website
page notice
Error-Message Guidelines
Progressive Enhancement
page notice
inline error notice
Error-Message Guidelines
Page notice showing form validation errors.