Skip to curriculum
Jump to the editor

17.10. Validate a field on focus and blur

Easy
Topics
15–25 min

A sign-up form should tell people about a mistake when they finish a field, not while they are still typing the first letter, and not when they merely Tab through a field on the way to another. Once a message is showing, it should go away as soon as the value is fixed. All of that is decided by focus events, and since focus and blur do not bubble, one listener on the form needs their bubbling partners, focusin and focusout.

Write validateOnBlur(form, validators) in validate.js.

Signature

JAVASCRIPT
function validateOnBlur(form, validators) // returns a stop function
  • form: the form element.
  • validators: an object that maps a field's name to a function (value, form) returning an error message, or "" (or undefined, or null) when the value is fine.
  • Returns a function that removes every listener validateOnBlur added.

A field is an <input>, a <textarea> or a <select>. Buttons are not fields: a <button>, and an <input> whose type is button, submit, reset or image.

A field's message goes in the element whose id is the first id in the field's aria-describedby. A field with an error has aria-invalid="true"; a valid field has no aria-invalid attribute and an empty message.

Requirements

  1. When focus leaves a field that has a validator, the field is validated and its message and aria-invalid are set (or cleared).
  2. A field that has never been validated, and whose value is the same as when it got focus, is not validated when focus leaves it: the user only passed through.
  3. Once a field has been validated, leaving it always validates it again.
  4. Typing never makes a new error appear. While a field shows an error, each input validates it again: the message updates, and disappears as soon as the value is valid.
  5. Validators receive the field's value and the form (so one field can be compared with another).
  6. Each field has its own state. Fields without a validator, and buttons, are left alone: a button is never validated, even when validators has a function for its name. Only an own property of validators is a validator: a field named toString or constructor has none unless validators itself defines it.
  7. All listeners are on the form: fields added later work too.
  8. The returned function removes every listener; two forms work separately.

Examples

JAVASCRIPT
const stop = validateOnBlur(signup, {
  email: (value) => (value.includes("@") ? "" : "Enter an email address."),
  confirm: (value, form) => (value === form.elements.password.value ? "" : "The passwords do not match."),
});

// Tab into Email and out again without typing   → no message
// type "ada", then Tab out                      → "Enter an email address.", aria-invalid="true"
// go back and type "ada@" (each keystroke)      → the message disappears at "@"
JAVASCRIPT
// <input name="email" aria-describedby="email-error email-help">
// the message goes in #email-error; #email-help is not touched
stop(); // nothing reacts any more

Environment

  • Plain JavaScript in a browser page; the tests build their own forms and dispatch focus changes in the order a browser fires them: blur then focusout on the field that loses focus, focus then focusin on the one that gets it. They type by setting value and dispatching a bubbling input event.
  • element.focus() and document.activeElement are not used by the tests, because a test frame may not have the page's focus.
  • Validation on submit and the built-in constraint API (setCustomValidity) are out of scope.

Hints

  1. What does the field need to remember between focusin and focusout, and what does it need to remember for the rest of the page's life?
  2. On input, how can you tell whether the field currently shows an error?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…