Menu

setCustomValidity() Explained: Native Form Error Messages
📰

setCustomValidity() Explained: Native Form Error Messages

DEV Community: html·Parsa J·8 days ago
#HCy3yDDE
#devto2#devto#dev#confirmpassword#submit
Long article (1079 words) — no AI summary yet.
Reading 0:00
15s threshold

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) .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.