Try to submit a signup form with an empty required field in Chrome and you get a little gray bubble: "Please fill out this field." Do the same thing in Firefox and the wording is different. Safari's looks different again. None of the three can be restyled from your CSS, none of them can be translated without translating the browser itself, and none of them match the rest of your form's copy.

So most teams do the obvious thing: add novalidate to the <form>, write their own validation in JavaScript, track an error state per field, wire up aria-live so screen readers hear about it, and manually move focus to the first bad field on submit. It works. It's also several hundred lines reimplementing something the browser was already going to do correctly — for free — if you'd just told it what to say.

The part nobody reaches for: setCustomValidity()

Every form control that participates in constraint validation — <input>, <select>, <textarea>, <button> — has a method called setCustomValidity(message). Call it with a non-empty string, and that string becomes the control's validation message. Everything else about native validation keeps working exactly as before:

const confirmInput = document.querySelector("#confirmPassword"); confirmInput.addEventListener("input", () => { if (confirmInput.value !== passwordInput.value) { confirmInput.setCustomValidity("Passwords don't match."); } else { confirmInput.setCustomValidity(""); } }); 

"Passwords match" isn't something type="password" or pattern can express on its own — it's a relationship between two fields, not a shape one field has to match. setCustomValidity() is how you teach the browser's own validation system a rule it doesn't ship with, instead of building a second, parallel validation system next to it.

Call form.reportValidity() on submit (or just let the form submit normally — the browser calls it for you unless novalidate is set), and if confirmPassword has a custom message, the browser focuses that field and shows your text in its native bubble. Screen readers announce it the same way they'd announce "please fill out this field," because as far as the accessibility tree is concerned, nothing about the mechanism changed — only the words did.

🎮 Try it yourself

▶️ Open the interactive playground →

Runs right in your browser — poke at it and watch the concept react live.

The gotcha that makes this worse than doing nothing

Here's where it bites people. setCustomValidity() doesn't expire, doesn't re-check itself, and isn't cleared automatically when the input changes:

// DON'T do this — only ever called once, in the submit handler form.addEventListener("submit", (e) => { if (confirmInput.value !== passwordInput.value) { confirmInput.setCustomValidity("Passwords don't match."); e.preventDefault(); } // missing: clearing it back out when the values DO match }); 

Set a non-empty custom message once, and confirmInput.validity.valid is false from that point forward — even after the user goes back and fixes the mismatch — because nothing ever told the browser the problem went away. The next submit attempt fails the exact same way, the user retypes an already-correct password over and over, and nobody can tell why. A form that silently refuses to submit is worse than one with no validation at all, because at least "no validation" lets the request through.

The fix is the one line in the first example that's easy to skip: call setCustomValidity("") the moment the condition that caused the error stops being true, every time, not just on submit — typically on every input event, not just on submit. An empty string means "no custom error"; it's not optional cleanup, it's half the API.

checkValidity() vs reportValidity()

Two methods ask "is this valid," and they do different amounts of work:

  • checkValidity() returns true/false and fires an invalid event on any failing control — but shows nothing to the user. Use it when you want the answer without the native UI, e.g. to disable a submit button reactively.
  • reportValidity() does the same check, and if it fails, also shows the browser's validation bubble and moves focus to the first invalid control — the same thing that happens automatically when a form is submitted without novalidate.
submitButton.disabled = !form.checkValidity(); // silent — just toggles a button // vs. saveButton.addEventListener("click", () => form.reportValidity()); // shows the bubble, focuses the field 

A button outside the <form> element — in a modal footer, say — can still trigger this with form.requestSubmit(), which runs the same validation reportValidity() would and only proceeds to the submit event if it passes.

Why bother, instead of a validation library

None of this replaces a library for genuinely complex, cross-field, async validation (checking a username against the server, say). What it replaces is the much more common case: a handful of fields where the only problem is that the browser's own wording doesn't match your product's voice, or where one rule — "these two fields must match," "pick at least one checkbox" — doesn't fit a built-in type or pattern. For that case, the browser already has focus management, screen-reader announcement, and :invalid styling working correctly. setCustomValidity() is the one line that lets you keep all of it and just change what it says.

🧠 Test yourself

Think it clicked? Take the 7-question quiz →

Instant feedback, a hint on every question, and an explanation for each answer — right or wrong.

Next time a form bubble shows the browser's generic wording instead of yours, you now know the fix is one method call — and one more call to undo it. Which field in your current project is still fighting the browser instead of talking to it?

🚀 Want more like this? Every guide, playground, and quiz lives on bestpractic.org — open it and sign up free so the next one finds you.

Thanks for reading! Let's stay connected: