What is a client-side validation?

Client-side validation is checking what a visitor typed into a form in their browser, before the form is sent to a server.

The browser does much of it for you through HTML attributes: required, type="email", minlength, maxlength, min, max and pattern. If a field breaks a rule, the browser blocks the submit and shows a message next to the field. JavaScript can add checks that HTML cannot express, such as two fields having to match.

Its value is speed. The visitor learns about a mistake at once, without waiting for a round trip to a server, and fewer bad submissions are sent. A typed email address with no @ is caught before it becomes a message you cannot reply to.

It is a convenience and not a safeguard. Anything running in the browser can be switched off or bypassed by posting to the endpoint directly. Checks that matter must also run on the server.

html
<input type="email" name="email" required>
<input type="text" name="zip" pattern="[0-9]{5}" title="Five digits">
<textarea name="message" minlength="10" required></textarea>

Related