Client-Side vs Server-Side Form Validation
The browser is a usability layer
Native constraints give fast feedback and reduce ordinary mistakes. They operate on the current document and are not proof that a request is acceptable. This local example declares a quantity rule; it does not reserve stock.
<label> Tickets <input name="quantity" type="number" min="1" max="4" step="1" required value="2"></label>The browser accepts 2, but cannot establish stock availability or user entitlement.
Treat submitted values as untrusted
A hidden field is still client data. A server should calculate authoritative prices from its own product records and verify permissions independently. Here a hidden product identifier is a request parameter, not proof of a valid product or access.
<form> <input type="hidden" name="product" value="book-7"> <label> Quantity <input name="quantity" type="number" min="1" max="4" value="2"> </label></form>FormData contains product and quantity; neither value has been verified by a server.
Return a useful server error
A real server parses inputs, checks allowed types and ranges, verifies authorization and current business rules, then performs the action only if allowed. Use parameterized database queries and safe output handling separately; validation alone does not prevent injection or guarantee authorization. This markup models a server rejection when only one ticket remains. No actual server is included.
<label for="qty">Tickets</label><input id="qty" name="quantity" type="number" min="1" max="4" value="2" aria-invalid="true" aria-describedby="stock-error"><p id="stock-error">Only one ticket remains. Choose one.</p>The value passes native constraints while the modeled business-rule error remains visible.
Common mistakes and practice
For state changes use the correct HTTP method, HTTPS, server authorization and appropriate CSRF protection. Check all submitted values, not just visible fields. Do not expose sensitive internal details in error messages. Practice explaining which checks belong to the browser and which require trusted server state.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Client-Side vs Server-Side Form Validation — Knowledge Check
Apply the control concepts taught in this lesson.
Which check must depend on trusted server state?
Is a hidden field trusted because it is not visible?
Does successful browser validation prove authorization?
What should the server do if stock is insufficient?
Which practice complements validation against database injection?