Engineering 4 min read Updated
Client-side validation that never replaces the server
In short
Use the constraints HTML already provides, add clear custom messages where they help, and check every value again on the server.
Client-side validation tells people about a mistake before the form is sent, so they can fix it while the details are still on screen. Done well, it uses the constraints that HTML already provides and adds custom messages only where they help. The browser does less than it seems to, and the server must still check every value.
Start with the built-in constraints
HTML can check most input without any JavaScript. MDN describes these attributes:
- required: the field must be filled in before the form can be submitted;
- minlength and maxlength: the minimum and maximum length of a text value;
- min, max and step: the limits and increment for numeric and date input types;
- type: a preset kind of data, such as an email address, for which the browser checks a well-formed format;
- pattern: a regular expression that the value must match. A textarea does not support it.
A field that breaks one of these constraints is marked invalid. The browser then blocks submission and shows a message. Use the attributes first, because they need no script. Layout and question wording matter as much, and are covered in the article on forms people actually finish.
Validation is not a security measure
MDN states that client-side validation should not be treated as an exhaustive security measure. It is too easy to bypass, so a malicious user can still send bad data to your server. The rule is short: never trust data that the client sends to your server.
Client-side validation still earns its place, because people can correct a mistake at once, without waiting for a round trip to the server. Treat it as a convenience for the reader, and keep the same rules on the server.
Working on something similar in your own product? A few honest sentences are enough – we read every enquiry and reply by email. Apply with your idea
Custom messages replace the browser’s own
MDN lists two drawbacks of the browser’s own messages. There is no standard way to change their look with CSS, and they depend on the browser locale, so a page in one language can show an error in another.
The usual fix is a custom message. The setCustomValidity method takes a message and marks the field invalid while that message is shown. Passing an empty string makes the field valid again, so clear the message as soon as the value is correct. MDN’s example checks for a type mismatch on each input event and sets a message only when the type does not match.
Read the validity state in script
Each field exposes a validity state made of read-only flags. MDN lists these, among others:
| Flag | True when |
|---|---|
| valueMissing | The element has a required attribute but no value |
| typeMismatch | The value is not in the required syntax, for example an email address |
| patternMismatch | The value does not match the pattern |
| tooShort and tooLong | The value fails the minimum length, or exceeds the maximum length |
| rangeUnderflow and rangeOverflow | The value is below the minimum, or above the maximum |
| customError | A non-empty custom message has been set |
| valid | The element meets all of its constraints |
Use these flags to choose the message for each field. A missing value can say that the field is required, while a type mismatch can explain the expected format. Adding novalidate to a form turns off the browser’s error bubbles. You must then show errors yourself, but the validity flags stay available to your script.
A checklist for each form
- Add the built-in constraints to every field that needs them, starting with required.
- Write each message so that it says what is wrong and how to fix it. The principles are set out in the article on error messages that help.
- Apply the same rules on the server, and reject any data that breaks them.
- Check the form with the keyboard alone, and read each message aloud to confirm that it makes sense without the field around it.
- Check what happens without JavaScript. The form should still submit, and the server should still validate every value. The article on progressive enhancement explains why this matters.
This article describes browser behaviour as documented by MDN. Browsers differ in how they display messages, so test each form in the browsers your users actually use.
Common questions
- Is client-side validation enough to protect a form?
- No. MDN says it should not be considered an exhaustive security measure, because it is too easy to bypass. The server must validate every submitted value.
- Can I style the browser’s own error messages?
- Not with a standard method. MDN notes that browser messages cannot be styled with CSS in a standard way and depend on the browser locale. setCustomValidity lets you write your own message.
- Should I turn off the browser’s validation?
- Only if you show the errors yourself. Adding novalidate stops the browser’s error bubbles, but the validity flags remain available to script.
Sources
- Client-side form validation, MDN Web Docs. Accessed 24 November 2026
- ValidityState, MDN Web Docs. Accessed 24 November 2026
Further reading
-
Storing dates and time zones without surprises
Store moments in UTC, keep the local time and zone for future events, and use immutable PHP date objects with IANA zone names.
-
Forms people actually finish
People finish a form when each page asks one thing, every label stays visible and each error appears where they will look.
-
Progressive enhancement in practice: HTML first
Build the page so the main task works with HTML alone, then add CSS and JavaScript as improvements that never block the baseline.
Have an idea that should exist? Or a working product that needs more customers, a new website or a complete rework? Bring us anything. Apply with your idea