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.
requiredmeans the box can’t be left empty, andminandmaxset 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
invalidevent. The first one gets the focus and a bubble, such asPlease fill out this field.orValue 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, notclickon the Add button. A click listener that callspreventDefault()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. :invalidmatches a box that breaks a constraint right now, so an empty required box is red from the moment the page loads.:user-invalidmarks 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()returnsfalsewhen a box fails and shows nothing.reportValidity()returnsfalsetoo, and shows the bubble. Both fire theinvalidevents, and neither sends the form. - A
novalidateattribute 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. Removenovalidateagain 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:
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: