Sub topic 1
Why validate forms, and where
Prerequisites
This chapter assumes you know basic HTML forms (form, label, input), can read simple CSS selectors, and can select elements and handle events in JavaScript. It also assumes you know that a form submission sends named values to a server in an HTTP request.
Two reasons to validate
Form validation checks that submitted data is acceptable before the application uses it. It serves two separate goals.
- Usability. A person who mistypes an email address should find out immediately, next to the field, with a message that says how to fix it. This is what client-side validation is good at.
- Security and data integrity. The server must not store or act on data that is malformed, out of range, or hostile. This is what only server-side validation can guarantee.
The two goals often use the same rules, but they have different owners and different guarantees. Mixing them up is the most common validation mistake.
Why the client cannot be trusted
Code that runs in the browser is under the user's control. Anyone can open developer tools, delete a required attribute, disable JavaScript, or skip the page entirely and send a request with curl or a script. Browser validation never runs in those cases.
curl -X POST https://example.com/signup \
-d "email=not-an-email" \
-d "username=A" \
-d "age=-5"This request never touches your form, so none of its checks apply. If the server accepts these values, the bad data is now in your system.
The rule that follows is simple. Client-side validation is a convenience. Server-side validation is the source of truth, and it must repeat every rule.
Where each check runs
Note the arrow from the script straight to the HTTP request. The server sits behind every path, so it is the only place a rule can be enforced for certain.
What each side is responsible for
| Concern | Client-side | Server-side |
|---|---|---|
| Fast feedback without a round trip | Yes | No |
| Guides the user to fix mistakes | Yes | Yes, after submission |
| Protects the database and application | No | Yes |
| Can be bypassed by the user | Yes | No |
| Can check against stored data (for example, a taken username) | Not alone | Yes |
Validation is not the only defense
Validation decides whether a value is acceptable. It does not make a value safe to place anywhere. Even a valid value must still be handled safely when it is used:
- Pass values to the database through parameterized queries, not string concatenation.
- Escape or encode values when writing them into HTML.
- Validate type and format on the server, even for fields the form limits with
typeorpattern.
Treat validation as one layer, with output encoding and safe queries as separate layers.
Common errors
- Relying on
requiredorpatternalone. These attributes are bypassed by any direct request. - Writing different rules on each side. If the client allows 3 to 20 characters and the server allows 3 to 30, users see confusing behavior. Keep the rules in step.
- Rejecting valid input to be safe. Overly strict patterns, such as refusing a
+in an email address or a space in a surname, frustrate real users without adding security. - Showing vague messages. "Invalid input" does not tell people what to change. Say what is wrong and what is allowed.
What comes next
The following chapters build the client side layer by layer: the built-in constraint attributes, input types, the Constraint Validation API, and accessible error messages. Later, you return to the server to validate every submitted value and send clear errors back to the page.
Exercise
A signup form marks the username field required and pattern="[a-z0-9_]+". A teammate says the server can skip checking the username because the browser already enforces it. Give one concrete way the server could still receive an invalid username, and say what the server should do.
Show answer
A user can send a request directly with a tool such as curl, or remove the attributes in developer tools, so the browser check never runs and a value like "A B!" arrives. The server should validate the username against the same rule on every request, reject invalid values, and return an error message for the username field.
Exercise
Which goal does client-side validation mainly serve, and which goal requires server-side validation?
Show answer
Client-side validation mainly serves usability, by giving fast and specific feedback. Security and data integrity require server-side validation, because only the server controls what is accepted and stored.