JavaScript Recap
No new theory, just a challenge that uses the JavaScript of this part: reading a box, the input and change events, submit and preventDefault, trim, includes and its friends, slice and indexOf, regular expressions and test, pattern, a message next to its box, setCustomValidity, aria-describedby and aria-invalid.
This time, nobody tells you which lines to write. Read what the passenger needs, and put it together yourself. When you're not sure how to do a part, open the lesson it comes from again.
Mission: Help with the badge
What you will see: type OS482 in the badge number box, then press Tab to go to the next box. Right under the badge number box, the page says:
Two capital letters and four digits, like OS4821
Go back, change it to OS4821, and leave the box again: the message is gone.
The browser refuses a wrong badge number, but only when the passenger clicks Book my seat, and its bubble goes away after a moment. The station wants the badge number box to explain itself, in words that stay, as soon as the passenger leaves it.
- Right after the badge number box, add an element with the id
badge-error. It's empty when the page opens. - When the passenger leaves the badge number box with a number that isn't two capital letters and four digits, it says
Two capital letters and four digits, like OS4821. - When the passenger leaves the box with a right number, the message goes away.
- A screen reader reads the message with the badge number box.
- A screen reader hears that the box is wrong while the message shows, and only then.
- Everything else on the form and on the pass works as before.
A form the browser refuses never gets its submit event.