HTML5 Form Validation
HTML5 Form Validation
Validation in Layers — what and why
required, type="email", pattern, min/max). This chapter assembles them and adds the one idea that separates hobby forms from real ones: the client-vs-server rule.The Built-in Validators (one-screen recap)
<input required> must not be empty
<input type="email"> must look like a@b.c
<input type="url"> must look like https://...
<input type="number" min="0" max="100"> must be 0-100
<input pattern="[6-9][0-9]{9}"> must match your regex
<input maxlength="6"> can't exceed 6 chars
<input type="date" min="2008-01-01"> must be on/after that date
The browser validates on submit by default, halting submission and pointing at the problem. That is genuine validation, free, in every modern browser.
The Golden Rule — Client vs Server (the whole chapter in one box)
CLIENT-SIDE (HTML5/JS in the browser):
✅ instant feedback, great experience, no page reload
❌ can be BYPASSED in seconds (DevTools removes 'required',
or a request is sent directly without the form)
SERVER-SIDE (PHP checking again after submit):
✅ the ONLY validation an attacker cannot skip
❌ needs a round-trip to the server
required, and submit — or skip the form entirely and POST raw data. So the server (your PHP) must check everything again. This single principle is the most common senior-level interview question about forms. It is also why our PHP/AJAX course at the end of this path re-checks every field.<form novalidate> ← turns OFF browser validation (used when you do it all in JS)
Styling Invalid Fields — a taste of CSS validation
<style>
input:required { border-left: 3px solid orange; }
input:invalid { border-color: red; }
input:valid { border-color: green; }
</style>
These :valid, :invalid, :required hooks are CSS pseudo-classes (full coverage in our CSS course). Even this tiny taste shows the payoff of choosing correct input types: the browser already KNOWS which fields are valid, so styling them is one line each.
Full Validated Admission Form
<form action="save-admission.php" method="post">
<fieldset>
<legend>Admission Form 2026</legend>
<label for="nm">Student Name:</label>
<input id="nm" name="student_name" required minlength="3"><br>
<label for="em">Email:</label>
<input id="em" type="email" name="email" required><br>
<label for="mo">Mobile:</label>
<input id="mo" type="tel" name="mobile"
pattern="[6-9][0-9]{9}" title="10-digit mobile" required><br>
<label for="cl">Class:</label>
<select id="cl" name="class" required>
<option value="">-- Select --</option>
<option value="9">9</option><option value="10">10</option>
</select><br>
<label for="db">Date of Birth:</label>
<input id="db" type="date" name="dob"
min="2007-01-01" max="2016-12-31" required><br>
<button type="submit">Submit Application</button>
</fieldset>
</form>
Every field carries its own guard: required stops blanks, type="email" checks shape, pattern enforces the mobile format, select's required + empty first option forces a real choice, date min/max fences the admission age. This is a complete, professional front-end form — and on the server, PHP will check all of it once more.
Exam Corner
Q: What does novalidate do? Disables the browser's built-in validation for that form.
Q: How to require a minimum length of 3 characters?
minlength="3" (and maxlength for the upper bound).Q: Which CSS pseudo-classes style validation state? :valid, :invalid, :required, :optional.
Q: One line to define good form validation practice? Validate on client for UX, re-validate on server for security.
Validation परतों में — क्या और क्यों
required, type="email", pattern, min/max). यह chapter उन्हें जोड़ता है और वह एक idea जोड़ता है जो शौकिया forms को असली forms से अलग करता है: client-vs-server rule.Built-in Validators (एक-screen recap)
<input required> khali nahi hona chahiye
<input type="email"> a@b.c jaisa dikhna chahiye
<input type="url"> https://... jaisa
<input type="number" min="0" max="100"> 0-100 ke beech
<input pattern="[6-9][0-9]{9}"> aapke regex se match
<input maxlength="6"> 6 char se zyada nahi
<input type="date" min="2008-01-01"> us date par/baad
Browser default रूप से submit पर validate करता है, submission रोककर problem पर इशारा करता है. यह असली validation है, मुफ्त, हर modern browser में.
Golden Rule — Client vs Server (पूरा chapter एक box में)
CLIENT-SIDE (browser me HTML5/JS):
✅ turant feedback, badhiya experience, page reload nahi
❌ seconds me BYPASS ho sakta hai (DevTools 'required' hata deta hai,
ya form ke bina seedha request bhej di jaati hai)
SERVER-SIDE (submit ke baad PHP dobara check karta hai):
✅ EKMATRA validation jise attacker skip nahi kar sakta
❌ server tak round-trip chahiye
required हटाकर submit कर सकता है — या form छोड़कर सीधा raw data POST कर सकता है. इसलिए server (आपका PHP) सब कुछ दोबारा जांचे. यही एक principle forms का सबसे common senior-level interview सवाल है. इसी वजह से इस रास्ते के अंत वाला हमारा PHP/AJAX course हर field दोबारा check करता है.<form novalidate> ← browser validation BAND kar deta hai (jab sab JS me karna ho)
Invalid Fields Style करना — CSS validation की झलक
<style>
input:required { border-left: 3px solid orange; }
input:invalid { border-color: red; }
input:valid { border-color: green; }
</style>
ये :valid, :invalid, :required hooks CSS pseudo-classes हैं (पूरा coverage हमारे CSS course में). यह छोटी झलक भी सही input types चुनने का फायदा दिखाती है: browser पहले से JANTA है कौन-से fields valid हैं, तो उन्हें style करना एक-एक line का काम है.
पूरा Validated Admission Form
<form action="save-admission.php" method="post">
<fieldset>
<legend>Admission Form 2026</legend>
<label for="nm">Student Name:</label>
<input id="nm" name="student_name" required minlength="3"><br>
<label for="em">Email:</label>
<input id="em" type="email" name="email" required><br>
<label for="mo">Mobile:</label>
<input id="mo" type="tel" name="mobile"
pattern="[6-9][0-9]{9}" title="10-digit mobile" required><br>
<label for="cl">Class:</label>
<select id="cl" name="class" required>
<option value="">-- Select --</option>
<option value="9">9</option><option value="10">10</option>
</select><br>
<label for="db">Date of Birth:</label>
<input id="db" type="date" name="dob"
min="2007-01-01" max="2016-12-31" required><br>
<button type="submit">Submit Application</button>
</fieldset>
</form>
हर field अपना पहरेदार लिए है: required खाली रोकता है, type="email" shape जांचता है, pattern mobile format enforce करता है, select का required + खाली पहला option असली choice मजबूर करता है, date min/max admission age की बाड़ लगाता है. यह पूरा, professional front-end form है — और server पर PHP इसे एक बार और जांचेगा.
Exam Corner
Q: novalidate क्या करता है? उस form की browser built-in validation बंद कर देता है.
Q: कम से कम 3 characters कैसे require करें?
minlength="3" (और ऊपरी सीमा के लिए maxlength).Q: कौन-सी CSS pseudo-classes validation state style करती हैं? :valid, :invalid, :required, :optional.
Q: अच्छी form validation practice एक line में? UX के लिए client पर validate, security के लिए server पर दोबारा validate.
💻 Live Code Editor
इस पेज का code यहाँ तैयार है — बदलिए और तुरंत नतीजा देखिए. कुछ install किए बिना.console.log देखने के लिए F12 दबाइए.