📘 Lesson  ·  Lesson 23

HTML5 Form Validation

HTML5 Form Validation

Validation in Layers — what and why

Validation = checking data is sane BEFORE it is accepted. A phone field getting "abcd", marks of 999, an empty required name — validation catches these. HTML5 does a huge amount with zero JavaScript: you have already met the pieces (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
On submit, the browser checks all of these, stops at the FIRST invalid field, focuses it, and shows a bubble message. No script written.

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
The rule every professional lives by: "Validate on the client for UX, re-validate on the server for security — NEVER trust the client alone." HTML5 validation is a helpful convenience for honest users; it is not a lock. Anyone can open DevTools, delete 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>
As the user types a correct email, the border turns green live; a wrong one stays red. Instant, wordless feedback.

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: Why is server-side validation necessary if HTML5 already validates? Because client validation is bypassable via DevTools or direct requests; only the server cannot be skipped.

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 परतों में — क्या और क्यों

Validation = data सही है यह accept करने से PEHLE जांचना. Phone field में "abcd", 999 marks, खाली required name — validation इन्हें पकड़ती है. HTML5 बिना JavaScript बहुत कुछ करता है: टुकड़े आप देख चुके हैं (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
Submit par browser ye sab check karta hai, PEHLE invalid field par rukta hai, use focus karta hai, aur bubble message dikhata hai. Koi script nahi.

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
वह rule जिस पर हर professional जीता है: "UX के लिए client पर validate करो, security के लिए server पर दोबारा validate करो — client पर KABHI अकेले भरोसा नहीं." HTML5 validation ईमानदार users के लिए सहूलियत है; ताला नहीं. कोई भी DevTools खोलकर 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>
User sahi email type karte hi border live green ho jaata hai; galat par red rehta hai. Turant, bina shabdon ke feedback.

ये :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: HTML5 पहले से validate करता है तो server-side validation ज़रूरी क्यों? क्योंकि client validation DevTools या direct requests से bypass हो जाती है; सिर्फ server skip नहीं हो सकता.

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.
← Back to HTML Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

इस पेज का code यहाँ तैयार है — बदलिए और तुरंत नतीजा देखिए. कुछ install किए बिना.
👁 Live Preview
सब कुछ आपके browser में ही चलता है — कोई server, कोई signup नहीं. JavaScript का console.log देखने के लिए F12 दबाइए.