How to stop bots from submitting forms

Intermediate3 min readUpdated

GET /learn/ad-fraud/how-to-stop-form-spam/ Accept text/html text/markdown

Short answer

Stop most form spam by layering cheap checks: a hidden honeypot field that only bots fill in, a signed timestamp that rejects forms sent too fast, strict server-side validation and rate limits, and a non-interactive challenge such as Turnstile on forms that matter. Add a content filter like Akismet for comment and contact spam, and double opt-in for any form that sends email.

Bots submit forms to post links for search rankings, to send phishing text through your autoresponder, to fill your CRM with fake leads and to sign victims up for mail floods. Most of them are scripts that post straight to the form's URL without loading the page, so any check that depends on the page being loaded, such as a hidden field, a signed token or a challenge, stops a large share of them.

Add a honeypot field

A honeypot is a field that people never see and bots fill in because they fill in everything. Hide it with CSS, not type="hidden", because many bots skip hidden inputs. Give it a name that browser autofill will not match (avoid email, name, phone or website), take it out of the tab order and hide it from screen readers:

html
<form method="post" action="/contact">
  <label for="email">Email</label>
  <input id="email" name="email" type="email" required>

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

  <div class="hp-field" aria-hidden="true">
    <label for="extra_info">Leave this field empty</label>
    <input id="extra_info" name="extra_info" type="text" tabindex="-1" autocomplete="off">
  </div>

  <input type="hidden" name="form_token" value="{{ form_token }}">
  <button type="submit">Send</button>
</form>

<style>
  .hp-field {
    position: absolute;
    left: -10000px;
    width: 1px;
    height: 1px;
    overflow: hidden;
  }
</style>

When the field arrives filled, show the normal thank-you page and discard the message, so the bot's operator does not learn which check caught it. Honeypots for bot detection covers trap links and other variants.

Reject forms sent too fast

People need a few seconds to fill in a form; scripts need none. Put the time the form was rendered into a hidden field, and sign it with an HMAC (a keyed hash) so a bot cannot write its own timestamp. Your template fills form_token with formToken(), and the handler rejects tokens that are missing, forged, younger than 3 seconds or older than 2 hours. Treat both limits as starting points.

js
import crypto from "node:crypto";

const SECRET = process.env.FORM_TOKEN_SECRET;
const MIN_AGE_SECONDS = 3;
const MAX_AGE_SECONDS = 2 * 60 * 60;
const EMAIL_PATTERN = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const LINK_PATTERN = /https?:\/\//gi;

function sign(timestamp) {
  return crypto.createHmac("sha256", SECRET).update(timestamp).digest("hex");
}

export function formToken() {
  const timestamp = String(Math.floor(Date.now() / 1000));
  return `${timestamp}.${sign(timestamp)}`;
}

function tokenAgeSeconds(token) {
  const [timestamp, mac] = String(token ?? "").split(".");
  if (!timestamp || !mac) return null;
  const expected = Buffer.from(sign(timestamp));
  const received = Buffer.from(mac);
  if (expected.length !== received.length || !crypto.timingSafeEqual(expected, received)) {
    return null;
  }
  return Math.floor(Date.now() / 1000) - Number(timestamp);
}

export function rejectReason(body) {
  if (body.extra_info) return "honeypot";
  const age = tokenAgeSeconds(body.form_token);
  if (age === null || age < MIN_AGE_SECONDS || age > MAX_AGE_SECONDS) return "timing";
  const email = String(body.email ?? "").trim();
  const message = String(body.message ?? "");
  if (email.length > 254 || !EMAIL_PATTERN.test(email)) return "email";
  if (message.length < 10 || message.length > 5000) return "length";
  if ((message.match(LINK_PATTERN) ?? []).length > 2) return "links";
  return null;
}

Call rejectReason(req.body) at the top of the form handler. Return the thank-you page for "honeypot", and a "please reload and try again" message for "timing" so a real person with an expired page can recover.

Validate everything on the server

Bots never run your browser-side validation, so the server is the only place a check counts. Enforce types and lengths, accept only known values for select fields, reject links in name and phone fields, and drop requests with an unexpected content type. Add a rate limit per IP address on the form endpoint. In nginx:

nginx
limit_req_zone $binary_remote_addr zone=forms:10m rate=5r/m;

server {
    location = /contact {
        limit_req zone=forms burst=3 nodelay;
        proxy_pass http://127.0.0.1:3000;
    }
}

The limit_req_zone line belongs in the http block. Five requests a minute per address with a small burst is a starting point for a contact form; signup forms under attack may need a limit per email address too.

Add a non-interactive challenge

For forms where spam gets past the cheap checks, add a challenge that runs in the background, such as Cloudflare Turnstile, reCAPTCHA v3 or Friendly Captcha. The widget gives the browser a token, and your server must verify it with the provider on every submission. Turnstile's server-side validation guide notes that tokens are single-use and expire after 300 seconds. CAPTCHA alternatives compares the options.

Filter the content

Some spam is typed or pasted by people, and no bot check stops it. A content filter scores the message itself. Akismet does this for WordPress comments and contact forms, and has an API for other platforms. Your own rules can catch the rest: messages with more than two links, the same message from many addresses, and phrases that keep appearing in spam you have already received.

Use double opt-in for anything that sends email

Any form that sends an email to the address typed in it can be used against a third party. Send one plain confirmation and nothing else until the person clicks it, leave user-typed fields such as first name out of that email, and do not auto-reply to contact forms with anything more than a bare acknowledgement. Subscription bombing explains why.

Tools that help

StopBots is published by Kairal, which is one of the tools listed. The list is not a ranking.

  • Akismet filters comment and contact form spam by content, the usual first step on a WordPress site.
  • Cloudflare Turnstile is a free challenge that most visitors pass without any interaction, and works on sites that do not use Cloudflare's CDN.
  • Friendly Captcha uses a proof-of-work puzzle solved in the background, a fit for sites that want an EU-based provider.
  • Google reCAPTCHA returns a risk score per submission that your server can act on.
  • Kairal sets protection per endpoint, such as signup and contact forms, and verifies suspicious visitors with invisible challenges, which suits sites where form abuse is one part of wider bot traffic.

Common questions

Is a honeypot or a CAPTCHA better against form spam?

A honeypot costs real users nothing and stops generic bots, but a bot written for your site can skip the field. A challenge stops more automated submissions at some cost in friction or privacy, so use the honeypot everywhere and add a challenge where spam still gets through.

Why do I still get spam with reCAPTCHA on my form?

Either the token is not checked on the server, so bots post to the form without it, or the spam comes from people or solving services that pass the challenge. Confirm that a submission without a valid token is rejected, then add content filtering and rate limits.

Should I reject form messages that contain links?

Rejecting every link blocks some real customers who paste a URL. A limit of one or two links per message, plus a ban on links in name and phone fields, is a common middle ground that you can tighten if spam keeps coming.