Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Let the Browser Check the Form

Your submit listener caught an empty name or a count that wasn’t a number, but a count of 0 or 50 still went on Sam’s list. In this video, you put Sam’s rules on the boxes themselves, watched the browser refuse a bad submit before your listener ran, and styled the boxes that need fixing. Here are the key points:

  • Constraint attributes state a rule on the box itself. required means the box can’t be left empty, and min and max set the smallest and largest number the form will send from a number box.
  • Before sending a form, the browser checks every constraint. Each box that fails fires an invalid event. The first one gets the focus and a bubble, such as Please fill out this field. or Value must be less than or equal to 20., and the form isn’t sent.
  • No submit event fires for a refused form, so your submit listener doesn’t run. Pressing Enter sends the form too, so the same checks run first.
  • That’s why the code listens for submit, not click on the Add button. A click listener that calls preventDefault() cancels the send, so the browser never checks the form and submit never fires. On a stripped-down page with a required Item box, clicking Add with the box empty put an empty row on the page and showed no bubble, and the submit listener never ran.
  • :invalid matches a box that breaks a constraint right now, so an empty required box is red from the moment the page loads.
  • :user-invalid marks an invalid box only after the user has changed it and left it, or tried to send the form. A box stays plain while you first type in it and turns red when you leave it invalid. An untouched empty box stays plain. From then on, it follows every edit, red while the value is invalid and plain as soon as it’s valid again. After a refused submit, every box that failed turns red.
  • You can ask whether the form would pass without sending it. A form’s checkValidity() returns false when a box fails and shows nothing. reportValidity() returns false too, and shows the bubble. Both fire the invalid events, and neither sends the form.
  • A novalidate attribute on the form skips the browser’s checks when the form is sent, so after you save and click Add with both boxes empty, submit fires and your listener’s own check catches the empty name. The constraints, the red borders and both methods still work. Remove novalidate again afterwards, so the browser checks the form before sending it.
  • Constraints help the user, but they don’t protect your data. Anyone with the page open can remove them. Your listener still checks the name and the number so your own code never stores a bad record, but that isn’t protection either: data that has to be trusted is checked again by the server that receives it. The 1 to 20 rule stays on the box alone, which is enough for Sam’s own list.

In kitchen/index.html, the two boxes in #add-form get their constraints, and the rest of the form stays as it was:

Language: HTML Filename: kitchen/index.html
<label>
  Item
  <input id="item-name" name="name" required>
</label>
<label>
  How many?
  <input id="item-count" name="count" type="number" required min="1" max="20">
</label>

In kitchen/style.css, a second rule gives a red border to each invalid box the user has changed and left, or that failed a refused submit:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Your submit listener checks for an empty name or a count that isn’t a number. Type eggs, then zero, and click Add. The list now holds zero eggs. A count of fifty gets through too.

00:15 Sam never buys fewer than one, or more than twenty. Instead of another check per rule, HTML can state these rules on the boxes, and the browser checks them.

00:27 Add the required attribute to the Item box. Then the box can’t be left empty. Give the How many box required too, plus min one and max twenty. Save, leave both boxes empty, and click Add.

00:44 A bubble under Item says Please fill out this field. Attributes like these are called constraints. Before sending a form, the browser checks every constraint.

00:55 Each box that fails fires an invalid event. The first one gets the focus and the bubble, and the form isn’t sent. No submit event fires, so your listener never runs.

01:07 Your turn: pause the video and predict what happens when you type eggs and twenty-five, then press Enter.

01:20 The bubble moves to How many: Value must be less than or equal to 20. Enter sends the form, so the checks run first. Change it to three, and Enter puts eggs on the list.

01:34 This is why your code listens for submit, not a click on Add. Here’s a stripped-down page, only for watching, with a required Item box and a click listener that cancels the default and adds a row.

01:48 Leave Item empty and click Add. An empty row appears, and there’s no bubble. The click’s preventDefault canceled the send, so the browser never checked the form, and the submit listener never ran.

02:02 A red border could mark each box Sam must fix. CSS has a pseudo-class for that, a word after a colon in a selector that matches an element by its state. The one whose name fits, :invalid, is a detour.

02:17 In style.css, give invalid inputs a red border, and save. Both boxes are red before Sam has typed a thing, so the form scolds Sam for nothing. :invalid matches any box that breaks a constraint right now.

02:34 An empty required box breaks one from the moment the page loads.

02:40 Change the selector to :user-invalid, and save. Both boxes start plain. It marks an invalid box only after you’ve changed it and left it, or tried to send the form.

02:52 Type twenty-five in How many: still plain. Press Tab to leave, and it turns red. Item is empty but plain. Sam hasn’t changed it. From then on, it follows every edit.

03:06 Go back, change it to two, and the red goes away. A refused submit counts too. Every box that failed turns red.

03:17 To check the form without sending it, call its checkValidity in the Console. It returns false and shows nothing.

03:25 Its partner, reportValidity, returns false too, and shows the bubble. Both fire the invalid events, and neither sends the form.

03:37 Add novalidate to the form, save, and click Add. Submit fires, and your listener catches the empty name. It only skips the checks on sending. Constraints, red borders and both methods still work.

03:51 Constraints help Sam, but they don’t protect your data. Anyone with the page open can remove them. Your listener still checks the name and the number. For Sam’s own list, one to twenty on the box is enough.

04:04 Now remove novalidate and save.

04:09 Put what HTML can state on the boxes as constraints, and the browser refuses a bad submit first. Use :user-invalid, not :invalid, for the red border. And keep your listener’s own checks.

04:24 Sam’s form now refuses an empty name and a count outside one to twenty. Refusing an item that’s already on the list is still missing, because HTML can’t express it.

04:35 Next, in Add Your Own Validation Rule, you’ll write it.

Become a Member to join the conversation.