Lesson 2 of 8

Form validation

Goal

Validate before you submit

01

Explanation

The browser can validate fields on its own: required keeps a field from being left empty, type="email" checks that the value looks like an email address, and minlength sets a minimum length, all without a single line of JavaScript.

That leaves two things for you: stop the default submission with event.preventDefault() so the page doesn't reload, and show the user a result they can understand.

With form.checkValidity() you ask the browser whether every field is valid, and base your response on its answer.

02

Your task

Make the email field required and set its type to email, stop the page from reloading, and show a message in #msg.

Checks

5 checks
  1. The field is of type email
  2. The field is required
  3. The default submission is blocked, so the page doesn't reload
  4. A valid email shows a success message
  5. An invalid email shows a different message
03

Hints

0/3
Track lessons0 of 8 done

Learn page layout, form validation, motion and accessibility: the things that make an interface comfortable to use in real life, not just nice to look at.

Advanced level, 8 lessons, 2 hours in total.

index.html
Preview
index.html100%
–/5

Press “Run and check” to grade your code.

5 checks listed under “Your task”.

Track lessons0 of 8 done

Learn page layout, form validation, motion and accessibility: the things that make an interface comfortable to use in real life, not just nice to look at.

Advanced level, 8 lessons, 2 hours in total.