Client-side validation is not enough

The required and pattern attributes help honest visitors but can be skipped with one command. Here is how to add server-side validation as well.

You added required to every field and a pattern to the phone number, and the browser now refuses to submit an incomplete form. Then an empty submission shows up in your inbox anyway, or one with nonsense in the email field.

Nothing is broken. Browser validation was never able to guarantee what reaches your server. This guide shows why, what server-side validation is, and how to show its errors to your visitors.

What client-side validation does

Client-side validation is any check that runs in the visitor's browser before the form is sent. HTML has several built in:

<form action="https://api.formdb.io/f/your-form-id" method="POST">
  <label for="email">Email</label>
  <input id="email" type="email" name="email" required>

  <label for="code">Order code</label>
  <input id="code" type="text" name="code" pattern="[A-Z]{3}-[0-9]+" required>

  <label for="message">Message</label>
  <textarea id="message" name="message" minlength="10" required></textarea>

  <button type="submit">Send</button>
</form>

required blocks empty fields, type="email" checks for something shaped like an email address, pattern tests the value against a regular expression (a compact way to describe a text format), and minlength sets a minimum number of characters.

These checks are worth having. They give instant feedback, need no network request, and work with screen readers. They exist to help people who are trying to fill in the form correctly.

Why browser validation can be bypassed

The checks run on a device you do not control. The form's action URL is the real entry point, and anyone can send a request to it without using your form at all.

The quickest way to see this is with curl, a command-line tool for making HTTP requests:

curl -i -X POST https://api.formdb.io/f/your-form-id \
  -H "Accept: application/json" \
  -d "email=not-an-email"

That sends a submission with an invalid email address and no message. The browser, with its required attributes, was never involved.

There are other routes to the same result:

  • Open the browser's developer tools and delete the required attribute from the page.
  • Add novalidate to the form element in the developer tools.
  • Use an old or unusual browser that ignores an attribute.
  • Run a bot that reads the action URL and posts to it directly, which is how most form spam is sent.

JavaScript validation has the same weakness. It is code running in the visitor's browser, and the visitor can skip it.

What server-side validation is

Server-side validation repeats the checks on the server that receives the submission, after the data has left the visitor's device. The sender cannot remove it, because it does not run on their machine.

The two kinds of validation do different jobs:

Client-side

Server-side

Runs

In the visitor's browser

On the receiving server

Purpose

Fast, friendly feedback

Guaranteeing what gets stored

Can be skipped by the sender

Yes

No

You want both. The browser catches honest mistakes early. The server decides what is accepted.

Server-side validation without writing a server

If your form posts to your own server, validation is code you write in the request handler. If you use a form backend, the service does it for you.

In FormDB you describe the form's fields in the form builder: each field has a name, a type such as email, number or date, a required switch, and optional rules. Every submission to the endpoint is then checked against that description on the server, however the request was sent.

Some of what the server enforces:

  • Required fields must be present and not blank.
  • Email, URL, number and date fields must contain a valid value of that type.
  • Dropdown, radio and checkbox values must be one of the options you defined.
  • Rules such as minimum and maximum length, a pattern, allowed email domains, number ranges, date ranges and file size limits.

Several rules have no HTML attribute at all, so the server is the only place they can be enforced. Allowed email domains, "must not contain" and "no weekends" are examples. The complete list is in validation rules.

One point to be aware of: FormDB only validates fields that are defined in the builder. Fields it does not know about are accepted and stored as sent. If a field matters, add it to the builder. The form builder guide explains how.

What a 422 validation response looks like

When a submission fails validation, FormDB does not store it and answers with HTTP status 422. Assume the builder defines a required Email field and a required Message field. The curl command above then returns:

{
  "ok": false,
  "error": {
    "code": "validation_failed",
    "message": "Email must be a valid email address. (+1 more)",
    "fields": {
      "email": { "message": "Email must be a valid email address.", "rule": "type" },
      "message": { "message": "Message is required.", "rule": "required" }
    }
  }
}

The parts are:

  • error.message is a one-line summary: the first problem, plus a count of any others.
  • error.fields has one entry per failing field, keyed by the field's name.
  • Each entry has a readable message and the rule that failed, such as required, type or minLength.

A successful submission returns status 201 with {"ok":true,"id":"..."}.

If the form is submitted as a normal page post, without JavaScript, the visitor sees a hosted "Submission failed" page with the summary message and a link back to the form. The per-field detail is only available in the JSON response.

How to show server errors next to each field

To display the errors in your own page, send the form with fetch and ask for JSON with the Accept header. This example puts each message under its field:

<form id="contact" action="https://api.formdb.io/f/your-form-id" method="POST">
  <label for="email">Email</label>
  <input id="email" type="email" name="email" required>
  <p class="field-error" data-error-for="email"></p>

  <label for="message">Message</label>
  <textarea id="message" name="message" required></textarea>
  <p class="field-error" data-error-for="message"></p>

  <button type="submit">Send</button>
  <p id="form-status" role="status"></p>
</form>

<script>
  const form = document.querySelector("#contact");
  const formStatus = document.querySelector("#form-status");

  form.addEventListener("submit", async (event) => {
    event.preventDefault();

    form.querySelectorAll("[data-error-for]").forEach((el) => {
      el.textContent = "";
    });
    formStatus.textContent = "Sending...";

    try {
      const response = await fetch(form.action, {
        method: "POST",
        headers: { Accept: "application/json" },
        body: new FormData(form),
      });
      const body = await response.json();

      if (response.ok) {
        form.reset();
        formStatus.textContent = "Thanks, we received your message.";
        return;
      }

      formStatus.textContent = body.error.message;

      const fields = body.error.fields || {};
      for (const [name, detail] of Object.entries(fields)) {
        const target = form.querySelector(`[data-error-for="${name}"]`);
        if (target) target.textContent = detail.message;
      }
    } catch (err) {
      formStatus.textContent = "Could not reach the server. Please try again.";
    }
  });
</script>

A few notes on the code:

  • The error text lives at body.error.message, not body.message.
  • error.fields is only present for validation errors. Other errors, such as a failed captcha, have a code and message only, which is why the code falls back to an empty object.
  • The HTML attributes stay in place. Honest visitors still get instant browser feedback, and the server response covers everything else.

Keep both layers in agreement

The most common validation bug is a mismatch: the browser allows a value the server rejects, or the other way round. Two habits help.

  • Define the rule once on the server, then mirror it in HTML where an attribute exists. FormDB's Setup tab generates form code from your builder fields and adds the matching attributes, such as required, minlength, maxlength, pattern, min and max.
  • Always handle the server's error response in your JavaScript, even if you believe the browser checks make it unreachable. They do not.

Next steps

Read validation rules for every rule and its error message, and JavaScript and AJAX for more on handling responses. Server-side validation is included on every plan, so you can create a free account and run the curl command above against your own form to watch it get rejected.

Your form can be livebefore your coffee is cold.

Create a form, paste one URL and send yourself a test. The Free plan covers 250 submissions a month, with no credit card required.