17.10. Validate a field on focus and blur
Topics
This question is in
17. DOM: changing the page and eventsA 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
function validateOnBlur(form, validators) // returns a stop functionform: the form element.validators: an object that maps a field'snameto a function(value, form)returning an error message, or""(orundefined, ornull) when the value is fine.- Returns a function that removes every listener
validateOnBluradded.
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
- When focus leaves a field that has a validator, the field is validated and its message and
aria-invalidare set (or cleared). - 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.
- Once a field has been validated, leaving it always validates it again.
- Typing never makes a new error appear. While a field shows an error, each
inputvalidates it again: the message updates, and disappears as soon as the value is valid. - Validators receive the field's value and the form (so one field can be compared with another).
- Each field has its own state. Fields without a validator, and buttons, are left alone: a button is never validated, even when
validatorshas a function for itsname. Only an own property ofvalidatorsis a validator: a field namedtoStringorconstructorhas none unlessvalidatorsitself defines it. - All listeners are on the form: fields added later work too.
- The returned function removes every listener; two forms work separately.
Examples
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 "@"// <input name="email" aria-describedby="email-error email-help">
// the message goes in #email-error; #email-help is not touched
stop(); // nothing reacts any moreEnvironment
- Plain JavaScript in a browser page; the tests build their own forms and dispatch focus changes in the order a browser fires them:
blurthenfocusouton the field that loses focus,focusthenfocusinon the one that gets it. They type by settingvalueand dispatching a bubblinginputevent. element.focus()anddocument.activeElementare 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
- What does the field need to remember between
focusinandfocusout, and what does it need to remember for the rest of the page's life? - 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