Basics

Client-side vs server-side validation — and why there's no real choice

Illustration: a cart rolls straight through a paper gate and stops at a steel one

"Client-side or server-side validation" sounds like a choice. Like "React or Vue".

But there's no choice here. The server is always mandatory, the client is a pleasant extra. The real question is different and far more practical: how do you describe the rules once instead of maintaining two lists that drift apart? We'll get there.

What each one actually does

These aren't two ways of doing one thing. They're two different jobs that happen to share a name.

Browser checking is about answering a human fast. It turns a field red before submission, stops the button working on an empty email, saves a second of waiting. That's it. It's interface.

Server checking is about what reaches your database. It's the only thing standing in the data's path. There's no going around it.

The difference shows up in one command. The form doesn't even have to be opened:

curl -X POST https://your-site.com/api/orders -d '{"quantity":-5}'

No browser. No required. Your API simply received a request, and everything from here is up to the server.

OWASP leaves no room for interpretation: validation "must be implemented on the server-side ... as any JavaScript-based input validation performed on the client-side can be circumvented by an attacker".

Comparing on the criteria that actually matter

Response speed. Client — instant, no network. Server — after submission, with a visible delay. The client wins outright here, and that's its whole point.

Data protection. Client — zero. Server — total. No bypass.

Behaviour on a bad connection. Client — tells you immediately. Server — the person waits and doesn't know whether anything arrived.

Who can disable it. Client — anyone, via devtools or a request that skips the form. Server — nobody but you.

What an attacker sees. Client-side rules sit in the page source and can be read in full. Server-side rules aren't visible — one more reason not to shuttle them back and forth.

Cost of a mistake. Miss a client check and someone is mildly inconvenienced. Miss a server check and you get junk in the database, negative quantities, duplicate accounts.

Who should do what — a straight answer

By situation, no hedging.

  • First project, limited time. Server first. A form without highlighting is ugly but works; an app without server validation doesn't work — you just don't know it yet.
  • App runs locally, only for you. Still the server. You're not an "attacker", but you'll make your own typo in the "price" field.
  • Internal tool behind a corporate login. Still the server. Login answers "who are you", not "what did you send".
  • A ten-field form people fill in by hand. Both. Here client-side checking pays for itself immediately — without it people submit five times.
  • A public API with no interface. Server only; there is no client.

There's a third place too — the database

It usually gets forgotten in the "client vs server" argument, and it's the last line that holds even when your code is wrong.

  • NOT NULL — the field cannot end up empty, whatever arrives.
  • UNIQUE — a second signup on the same email physically cannot happen.
  • CHECK (quantity > 0) — a negative quantity is never written.

Why bother if the server already checks? Because over time you accumulate several server paths to the database: the form handler, an import script, a data migration, a manual query in the console at 3am. A check in code can be walked around by accident. A constraint in the database can't.

UNIQUE earns its keep especially: without it, two simultaneous requests will happily create two accounts on one email, and no "does this already exist?" check in code will stop them — time passes between the check and the write.

How not to write the rules twice

This is where the real engineering starts. Two rule lists always drift: the client limit gets raised to 5000 characters, the server stays at 2000 — and people get rejected after submitting, with no idea why.

The fix is to keep one schema and use it from both sides. If you have JavaScript on both ends (Next.js, Nuxt, SvelteKit) that's literally one file:

// shared/feedback.js — imported by the form and by the handler
export const Feedback = z.object({
  email: z.email(),
  message: z.string().trim().min(10).max(2000),
});

The form calls safeParse as you type, to highlight a field. The server calls the same thing before writing to the database. One rule, changed in one place.

If your stack is mixed — React and Python, say — the shared source is the API description itself (an OpenAPI schema), from which both checks are generated. More on that boundary in our explainer on frontend and backend, and the concrete server steps in the guide on how to validate user input.

If the server validates, can I drop the client check?

Technically yes, the app stays safe. But convenience suffers: every typo costs the person a full "submit — wait — read the error" cycle. On long forms that's a real loss.

And if AI writes both the frontend and the backend — will it do both?

Usually it does the client one: it's visible in the interface and it's what the prompt asked for. The server one has to be requested separately and explicitly — "add request body validation in the handler, before writing to the database". Checking is easy: send a curl request past the form and look at the status code.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →