Command Palette

Search for a command to run...

Hectal
Phase 13: Errors, Debugging and Testing
JavaScript · Exercise 4 of 5 PreviousNext
Intermediate

A sign-up form that explains its errors

When the form is submitted, stop the page from reloading and check the inputs with validateSignup(name, ageText):

- The name (after trimming) is required.

- The age must be a whole number from 1 to 120. Mention the word age in its error message.

If a check fails, validateSignup throws an error. Catch it in the submit handler and show err.message in #error, and empty #result.

If everything is valid, show Welcome, <name> (<age>) in #result, for example Welcome, Asha (12), and empty #error.

What Check tests

  1. An empty name shows an error
  2. Age abc shows an error about the age
  3. Age 200 shows an error about the age
  4. Age 4.5 is rejected
  5. Valid input shows Welcome, Asha (12) and clears the error
  6. No uncaught errors

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Your page

Press Run to see the page.

Console

Nothing logged yet.