Learn · HTML
HTML form validation, client and server
Written by Elis Hedman, Founder, Formward. Last reviewed
Browsers ship a complete validation system: declarative rules in attributes, a JavaScript API to query and extend them, and CSS hooks to style the result. Used well, it removes most client-side validation code. It also has a few sharp edges, and none of it replaces checking the data on the server.
Declarative rules
required, type (email, url, number, date...), min/max/step, minlength/maxlength and pattern all feed one model. Each failing rule sets a flag on the element's validity object (valueMissing, typeMismatch, patternMismatch, tooShort, rangeOverflow, customError and so on), and the browser refuses to submit while any control is invalid.
patternmust match the whole value: it is implicitly wrapped in^(?:and)$.- An empty value passes
pattern,minlengthandtype="email". Addrequiredif the field must be filled. patternis compiled with thevflag, so inside a character class a literal-,(,),[,{,},/or|has to be escaped.[a-z0-9._%+-]is a syntax error invmode; write[a-z0-9._%+\-].- An invalid pattern is not applied at all. There is no error on the page; the field just accepts anything.
type="email"follows the HTML spec's own definition, which is a deliberate violation of RFC 5322. It acceptsa@b(no dot required) and rejects some addresses RFC 5322 allows.
The constraint validation API
checkValidity() returns a boolean and fires invalid events; reportValidity() does the same and shows the browser's messages. setCustomValidity(message) marks a control invalid with your text; the control stays invalid until you call it again with an empty string. That makes it the tool for rules attributes cannot express, such as a confirmation field:
js
const email = form.elements.email;
const confirm = form.elements.email_confirm;
confirm.addEventListener("input", () => {
confirm.setCustomValidity(
confirm.value === email.value ? "" : "The two addresses do not match.",
);
});novalidate with your own messages
novalidate on the form turns off the browser's submit-time check and bubbles, but the whole API keeps working. That is the clean way to render your own inline errors while still using the browser's rules:
html
<form id="signup" novalidate>
<label for="email">Work email</label>
<input id="email" name="email" type="email" required aria-describedby="email-error">
<p id="email-error" class="field-error" hidden></p>
<button>Sign up</button>
</form>
<script type="module">
const form = document.querySelector("#signup");
form.addEventListener("submit", (event) => {
let firstInvalid = null;
for (const el of form.elements) {
if (!el.willValidate) continue;
const out = document.getElementById(el.id + "-error");
const ok = el.validity.valid;
el.toggleAttribute("aria-invalid", !ok);
if (out) {
out.hidden = ok;
out.textContent = ok ? "" : el.validationMessage;
}
if (!ok && !firstInvalid) firstInvalid = el;
}
if (firstInvalid) {
event.preventDefault();
firstInvalid.focus();
}
});
</script>validationMessage is the browser's localized text; swap in your own per validity flag if you need a specific tone. For styling, :user-invalid (Baseline since November 2023) matches invalid fields only after the user has interacted with them or tried to submit, which avoids painting a fresh form red.
Why the server still validates
Everything above runs in a browser you do not control. curl, a bot, devtools or a browser with JavaScript off skips it. Repeat every rule that protects data quality or your systems on the server, and return the failing field so a fetch() client can show it:
js
const RULES = {
email: { required: true, max: 254, email: true },
message: { required: true, max: 5000 },
};
function validate(body) {
for (const [name, rule] of Object.entries(RULES)) {
const value = String(body[name] ?? "").replace(/\r\n/g, "\n").trim();
if (rule.required && !value) return { field: name, code: "required" };
if (value.length > rule.max) return { field: name, code: "too_long" };
if (rule.email && value && !/^[^\s@]+@[^\s@]+$/.test(value)) {
return { field: name, code: "invalid_email" };
}
}
return null;
}
// const error = validate(req.body);
// if (error) return res.status(422).json({ ok: false, ...error });Note the CRLF normalization. During submission the browser converts newlines in values to CRLF, while maxlength counts the LF-only value in the page. A textarea at its maxlength with line breaks arrives longer than the limit you set, so compare against the normalized value or leave headroom.
An input the browser accepts and the server rejects
Some rules cannot be written as attributes, because the browser does not have the facts. A booking form is the usual case: type="date" and required make the browser check that a calendar date was entered, but only the server knows that the day is already full. The example below is generic and runs on your own machine. Its fully_booked code and its message are the example's own, not a Formward response.
The server is Node.js with no dependencies. It serves the page, and on POST /book it checks two things: the date format again, because the server cannot assume the browser's check ran, and then the booking rule.
js
import { createServer } from "node:http";
import { readFile } from "node:fs/promises";
// Test data: the one date with no slots left.
const FULLY_BOOKED = new Set(["2030-06-14"]);
createServer(async (req, res) => {
if (req.method === "GET") {
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
return res.end(await readFile(new URL("./index.html", import.meta.url)));
}
let raw = "";
for await (const chunk of req) raw += chunk;
const date = new URLSearchParams(raw).get("date") ?? "";
const send = (status, body) => {
res.writeHead(status, { "Content-Type": "application/json" });
res.end(JSON.stringify(body));
};
// Repeat the browser's rule first: the server cannot assume it ran.
if (!/^\d{4}-\d{2}-\d{2}$/.test(date)) {
return send(422, { ok: false, field: "date", code: "invalid_date", message: "Enter a date." });
}
// The rule only the server can check.
if (FULLY_BOOKED.has(date)) {
return send(422, { ok: false, field: "date", code: "fully_booked", message: "That day is fully booked. Pick another date." });
}
send(200, { ok: true, date });
}).listen(8787, "127.0.0.1");Save it as server.mjs next to the index.html from the next section and run node server.mjs. The test data is one date: 2030-06-14 is the full day, every other date is free. We ran curl -i -d "date=2030-06-14" http://127.0.0.1:8787/book and got 422 with {"ok":false,"field":"date","code":"fully_booked",...}. date=2030-06-15 answered 200, and date=tomorrow, which no date picker would send, answered 422 with invalid_date.
Show the server's error on the field
The page posts with fetch(), so there is no navigation and the input keeps its value. When the server answers 422, the script writes the message into the paragraph the input already points at with aria-describedby, marks the input with aria-invalid and moves focus back to it. The visitor reads what is wrong next to the field and only has to pick another date.
html
<!doctype html>
<meta charset="utf-8">
<title>Booking test</title>
<form id="booking" action="/book" method="post">
<label for="date">Date</label>
<input id="date" name="date" type="date" required aria-describedby="date-error">
<p id="date-error" role="alert" hidden></p>
<button>Book</button>
</form>
<p id="done" hidden>Booked.</p>
<script type="module">
const form = document.querySelector("#booking");
const error = document.querySelector("#date-error");
form.addEventListener("submit", async (event) => {
event.preventDefault();
const res = await fetch(form.action, {
method: "POST",
body: new URLSearchParams(new FormData(form)),
});
const data = await res.json();
error.hidden = res.ok;
error.textContent = res.ok ? "" : data.message;
form.elements.date.toggleAttribute("aria-invalid", !res.ok);
document.querySelector("#done").hidden = !res.ok;
if (!res.ok) form.elements.date.focus();
});
</script>To watch it happen, open http://127.0.0.1:8787/, open DevTools on the Network tab, pick 14 June 2030 and press Book. The browser lets the submit through, because the value satisfies required and type="date". The book request shows status 422: its payload is date=2030-06-14 and its response is the JSON with field and code. The message appears under the input, which still holds the date. Change it to 15 June and submit again: a second request with status 200, and the error is gone.
Two things change outside this small setup. When the form posts to another origin, the server has to allow your origin or the script cannot read the response at all: see CORS and forms. When the script does not run, the form's `action` attribute decides where the browser navigates, and the server has to answer with a page that shows the error and has the submitted values filled in. Timeouts, double submits and JSON bodies are covered in submitting a form with fetch().
Patterns on the server: watch for ReDoS
If your server runs regular expressions that someone else wrote (a CMS field setting, a form builder, a per-customer rule), a backtracking engine like JavaScript's can be made to take exponential time. (a+)+$ against a long run of a followed by ! is the textbook case, and on a single-threaded server one request stalls everyone.
Mitigations, cheapest first: cap the input length before matching, reject patterns with nested quantifiers or backreferences when they are saved, anchor patterns so the engine cannot retry at every offset, or use a linear-time engine such as RE2 when you need arbitrary patterns.
How this works with Formward
On hosted forms and Pages built in the Formward dashboard, field rules (required, email, number range, pattern) are enforced server-side on every submission, with a 422 carrying field and code when one fails. Custom patterns go through a save-time ReDoS guard that rejects backreferences, lookbehind, nested repetition such as (a+)+, ambiguous repeated alternation, more than three unbounded quantifiers and patterns over 200 characters. Matching is anchored, and values over 1,000 characters fail a pattern rule without the regex running.
For your own HTML form posting to the endpoint, Formward applies global caps (150 fields, 50,000 characters per value) and leaves field rules to your markup. See hosted forms and responses.
Sources
- MDN, Client-side form validation
- MDN, pattern attribute
- MDN, ValidityState
- MDN, setCustomValidity()
- MDN, :user-invalid
- WHATWG HTML, valid email address
- WHATWG HTML, the constraint validation API
- OWASP, Regular expression Denial of Service
- MDN, <input type="date">
- RFC 9110, 422 Unprocessable Content
- MDN, aria-invalid
Point a form at an EU endpoint
Formward receives the POST, filters spam and stores submissions in Sweden. No server to run.