Sub topic 5
How the browser reports errors and when to use novalidate
This chapter assumes you know basic HTML forms, how a form submission sends data to a server, and how to attach JavaScript event listeners. You should also have seen the constraint attributes (required, minlength, pattern, and so on) from earlier chapters.
What the browser does on submit
When a user activates a submit button, the browser runs interactive validation before it sends anything. The steps are:
- It checks every submittable control against its constraints.
- If all controls are valid, it fires the
submitevent and then sends the form. - If any control is invalid, it fires an
invalidevent at each failing control and stops. Nosubmitevent fires and no request is sent. - It focuses the first invalid control and shows a message bubble next to it.
The message text comes from the browser and is localized to the user's language. You cannot style the bubble with CSS, and its wording differs between browsers.
Which controls are checked
Only controls with willValidate set to true take part. These are not validated:
- Disabled controls.
- Controls with the
readonlyattribute. - Hidden inputs (
type="hidden"). - Buttons of
type="button"ortype="reset". - Controls inside a
<datalist>.
A common surprise is that required on a readonly field has no effect. The browser skips the field.
Skipping validation for one button
Sometimes a button should send the form without checking it, such as "Save draft". Use the formnovalidate attribute on that button.
<form action="/posts" method="post">
<label for="title">Title</label>
<input id="title" name="title" required>
<button type="submit">Publish</button>
<button type="submit" formnovalidate formaction="/posts/draft">Save draft</button>
</form>"Publish" validates the form. "Save draft" skips validation and posts to a different URL. The server must still accept incomplete data on that route.
The novalidate attribute
Adding novalidate to the <form> turns off interactive validation for the whole form. The browser no longer blocks submission and no longer shows bubbles. The constraints still exist. validity, checkValidity(), reportValidity(), and the CSS pseudo-classes continue to work.
<form id="signup" action="/signup" method="post" novalidate>
<label for="email">Email</label>
<input id="email" name="email" type="email" required>
<p id="email-error" role="alert"></p>
<button type="submit">Sign up</button>
</form>With novalidate, your own code decides what happens on submit. It can read each control's validity, write messages into the page, and call preventDefault() when something is wrong.
const form = document.getElementById('signup');
form.addEventListener('submit', (event) => {
if (!form.checkValidity()) {
event.preventDefault();
form.reportValidity();
}
});Here checkValidity() returns false if any control fails, and also fires invalid events. reportValidity() does the same check and then shows the browser bubble. With novalidate set, calling reportValidity() still shows bubbles, so you can mix the two approaches.
Choosing between default and novalidate
| Situation | Choice |
|---|---|
| Simple form, default messages are acceptable | Keep default validation |
| You need inline messages that match your design or support a screen reader flow | Use `novalidate` and write your own messages |
| You need to show all errors at once, not one bubble at a time | Use `novalidate` |
| One button must skip checks | Use `formnovalidate` on that button |
Common errors
- Adding
novalidateand doing nothing else. The form then submits invalid data with no feedback. Always pair it with a submit handler or rely on server errors. - Setting
novalidatefrom JavaScript only. If the script fails to load, the browser falls back to default validation. That is usually safe, but test it. - Treating bypassed validation as secure. Users can remove attributes in developer tools or send requests directly. The server must validate every value.
- Forgetting that
invaliddoes not bubble. Listen on the control itself, or use a capturing listener on the form.
Exercise
A form has novalidate, and its submit handler is empty. A user leaves a required field blank and clicks submit. What happens, and what two things would you add to fix the experience?
Show answer
The form is sent with the blank value, because novalidate disables the browser's blocking and bubbles. Add a submit handler that calls checkValidity() and preventDefault() when it fails, and show an error message for each invalid field (for example from validationMessage). The server must also reject the blank value.