Skip to content

Form validation that agrees in the browser and on the server

Check answers in the browser for speed and on the server for safety, from one set of rules, with errors shown next to each field.

2 min read

On this page

Form validation has two jobs that pull in different directions. In the browser it should be instant and friendly, telling people what to fix before they press send. On the server it must be strict, because anyone can skip your page and post straight to the endpoint. When the two sets of rules drift apart, people see errors the browser never warned them about, or bad data slips past the browser checks.

Why browser checks are not enough

HTML gives you a lot for free: required, type="email", minlength, maxlength and pattern all stop a submit until the answer fits. They are great for people. They do nothing against a script that sends a request directly, a browser with JavaScript turned off on a custom widget, or an old page cached with older rules. Every rule that matters has to be checked again on the server.

One set of rules, checked twice

In Formpik, each field carries its rules: required, fewest and most characters or words, a pattern with its own message, the most links an answer may hold, blocked words, work emails only, no throwaway mailboxes, date ranges, allowed weekdays and more. The endpoint checks every one of them on each submission. When the form's Refuse answers that break a rule setting is on, a broken rule refuses the submission with a 422 and one message per field:

{
  "ok": false,
  "message": "Please check the highlighted answers and try again.",
  "errors": {
    "email": "Please enter a valid email address",
    "message": "Please write at least 20 characters"
  }
}

The same rules also reach the browser. The HTML snippet for a source writes the matching attributes, such as required and maxlength, so most mistakes are caught before the form is sent.

Show server errors next to the field

The Formpik script reads that 422 answer and puts each message under its field, marks the field with aria-invalid, and moves focus to the first one, so keyboard and screen reader users land exactly where the fix is. Messages for fields your page does not have join the general message under the form. If you build your own form in JavaScript, read errors from the response and do the same.

Check a field before the form is sent

For rules a browser cannot know, such as blocked words or a throwaway email domain, ask the server early. The /validate endpoint runs the same checks, stores nothing and answers with the errors. Add data-formpik-validate="live" to the form and each field is checked as the visitor leaves it.

Rules that only the server can check

  • File size and type, because a file input's accept attribute only filters the picker.
  • One per answer, such as one sign-up per email address, which needs the submissions you already have. It is checked only on a real send, so the validate endpoint never reveals who signed up.

Write messages people can act on

  • Say what to do, not what went wrong: Use the email you check every day beats Invalid input.
  • Keep the visitor's answer in the field after an error, so they fix one thing instead of retyping everything.
  • Use a field's custom message for rules that need context, such as an order number pattern.

References

Keep reading

More from the blog