html · level 2 · unit 1 · lesson 3
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
required stops a form until the box is filled.
Add required to an input and the browser will not send the form while that box is empty. It points at the box and shows a message instead.
This checking is called . Until a required box has an answer, the browser treats it as . An email box is invalid too while what is typed has no @.
required needs no value: the word on its own is enough. Tell people as well, by adding (required) to the label, so nobody is caught out.
<form>
<label for="name">Name (required)</label>
<input id="name" name="name" required>
<label for="email">Email (required)</label>
<input type="email" id="email" name="email" required>
<button>Book a table</button>
</form> Press show my page to see it here.
Validation in the browser helps visitors, but the server should still check every answer again.
guess first
Which boxes start out invalid?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
<form>
<label for="name">Name (required)</label>
<input id="name" name="name" required>
<label for="nick">Nickname</label>
<input id="nick" name="nick">
</form>
your turn
Your turn. Make the key answers compulsory.
A new account needs an email and a password. Add required to both of those boxes, and leave the nickname optional.
Press show my page to see it here.
- the email is requirednot checked yet
- the password is requirednot checked yet
- the nickname stays optionalnot checked yet
fix it
The form goes without a guest name. Can you fix it?
Press show my page to see it here.
- the box is marked requirednot checked yet
- the empty name box is invalidnot checked yet
- The label says required, but the browser lets the form go. Which tag did
requiredend up on? requiredonly works on the box people type in, not on its label.- Move
requiredfrom the<label>on line 2 to the<input>on line 3.
nailed it
your form knows what it needs. :D
requiredstops a form until the box is filled.- Missing or wrongly shaped answers are invalid.
- Say (required) in the label too.
new words in your collection
- validation
- invalid
This lesson isn’t saved. A free account remembers every lesson you finish.