Practice validating user input in the browser.
Make sure you have Git and Node (v18) installed.
- Use this template, clone your copy, cd into it
- Run
npm install
to install all the dependencies - Open
challenge/index.html
in your browser
Each challenge has associated unit tests. You can either run all the tests with npm test
, or each individual challenge's tests with npm run test:1
, npm run test:2
etc.
Make sure you read test failures carefully—the output can be noisy but the error message should provide useful information to help you.
The form in challenge/index.html
can be submitted without filling out any of the fields. Without adding any JS make sure these fields are present:
- The
name
input - The
email
input - The
message
textarea
Make sure to mark these fields as required so the user knows they must fill them in.
Currently all the fields accept anything as input. Without adding any JS make sure each of these fields is restricted to its type:
- The
email
input should be a valid email - The
website
input should be a valid URL
Some users are sending excessively long messages. Without adding any JS makes sure the message
textarea only accepts up to 140 characters.
Make sure to tell the user about the limit so they aren't surprised when they try to submit.
Users keep shouting at us, and it makes us feel bad. Without adding any JS make sure the subject
input can only contain lowercase letters, numbers, and spaces.
The validity of each field is not consistently exposed to assistive technologies like screen readers. Write some JS in challenge/index.js
that uses the aria-invalid
attribute to help:
- Mark all fields as valid at first
- When a field fails validation mark it as invalid
- When a field is edited mark it valid again
The validation messages shown by the browser cannot by styled, and are not consistently exposed to assistive technologies. Write some JS in challenge/index.js
to add these messages to the DOM instead:
- Create a new element to hold each message
- Associate this element with its field
- When a field fails validation add the browser's default message to the element
- When a field is edited remove the message
It's a bit confusing for users now as the validation messages show up in two places: the default popup bubble and your new DOM element. Write some JS in challenge/index.js
to disable this built-in behaviour:
- Stop the
<form>
element validating fields - Manually check whether all the fields are valid
- If not prevent the form submission from taking place