दिन 7: Forms 2: Select, Textarea और Validation
✅ आज आप सीखेंगे
- select, option और optgroup से dropdowns
- datalist से suggestions
- textarea से कई lines का text
- fieldset और legend से fields का group
- Built-in validation: required, min/max, maxlength, pattern
🏢 असली दुनिया में
पिछले हफ़्ते के form से 40 enquiries आईं – पर 9 में mobile number ग़लत थे और 5 event की तारीख़ भूल गए। आज आप browser से ही ग़लत data भेजने से पहले रुकवाएँगे: JavaScript की ज़रूरत नहीं।
Dropdowns: select और option
<label for="occasion">Occasion</label>
<select id="occasion" name="occasion" required>
<option value="">– Choose –</option>
<optgroup label="Family">
<option value="wedding">Wedding</option>
<option value="birthday">Birthday</option>
</optgroup>
<optgroup label="Festivals">
<option value="diwali" selected>Diwali</option>
<option value="rakhi">Raksha Bandhan</option>
</optgroup>
<option value="corporate">Corporate gifting</option>
</select>
<br><br>
<label for="boxes">Box sizes (Ctrl/⌘ for many)</label><br>
<select id="boxes" name="boxes" multiple size="3">
<option>250 g</option><option>500 g</option><option>1 kg</option>
</select>datalist: लिखें या चुनें
<label for="city">Delivery city</label> <input id="city" name="city" list="cities" placeholder="Start typing…"> <datalist id="cities"> <option value="Jaipur"><option value="Ajmer"><option value="Kota"><option value="Udaipur"><option value="Jodhpur"> </datalist>
लंबे text के लिए textarea
<label for="note">Special instructions</label><br> <textarea id="note" name="note" rows="4" cols="40" maxlength="200" placeholder="e.g. less sugar, Jain (no onion-garlic) namkeen"></textarea>
fieldset और legend से grouping
<fieldset> <legend>Payment</legend> <label><input type="radio" name="pay" value="upi" checked> UPI</label> <label><input type="radio" name="pay" value="cash"> Cash on pickup</label> </fieldset> <fieldset disabled> <legend>Gift wrapping (coming soon)</legend> <label><input type="checkbox"> Add ribbon</label> </fieldset>
Built-in validation
| Attribute | नियम | उदाहरण |
|---|---|---|
| required | ख़ाली नहीं होना चाहिए | <input required> |
| min / max | Number या date range | min="5" max="200" |
| step | मान्य बढ़त | step="0.5" |
| minlength / maxlength | Text की लंबाई | minlength="3" |
| pattern | Regular expression | pattern="[6-9][0-9]{9}" |
| type="email" / "url" | Format जाँच | a@b.com |
| title | pattern error में hint की तरह दिखता है | title="10-digit mobile" |
<form onsubmit="alert('All fields valid – would be sent!'); return false;">
<p><label>Name* <input name="name" required minlength="3"></label></p>
<p><label>Mobile* <input name="mobile" type="tel" required pattern="[6-9][0-9]{9}" title="10-digit Indian mobile starting with 6–9" placeholder="9829012345"></label></p>
<p><label>PIN code <input name="pin" pattern="[1-9][0-9]{5}" title="6-digit PIN code"></label></p>
<p><label>Quantity (kg)* <input name="qty" type="number" required min="5" max="200" step="0.5"></label></p>
<p><label>Event date* <input name="date" type="date" required min="2026-01-01"></label></p>
<button>Send enquiry</button>
</form>[6-9][0-9]{9} = पहला अंक 6, 7, 8 या 9, फिर ठीक नौ और अंक। Pattern को पूरी value से मेल खाना चाहिए। PIN code: [1-9][0-9]{5}।
कोई भी HTML validation bypass कर सकता है (DevTools, scripts)। यह user experience सुधारता है; server को फिर भी सब दोबारा validate करना होगा।
और काम के attributes
<form novalidate> <p><label>Order id <input value="BK-1042" readonly></label> (readonly – sent)</p> <p><label>Coupon <input value="DIWALI20" disabled></label> (disabled – NOT sent)</p> <p><label>Name <input name="n" autofocus autocomplete="name"></label></p> <p><label>OTP <input inputmode="numeric" autocomplete="one-time-code" maxlength="6"></label></p> </form>
असली example: final enquiry form
<form action="https://httpbin.org/post" method="post" target="_blank">
<fieldset>
<legend>Your details</legend>
<p><label for="n">Name*</label><br><input id="n" name="name" required minlength="3" autocomplete="name"></p>
<p><label for="m">Mobile*</label><br><input id="m" name="mobile" type="tel" required pattern="[6-9][0-9]{9}" title="10-digit mobile" autocomplete="tel"></p>
</fieldset>
<fieldset>
<legend>Order</legend>
<p><label for="o">Occasion*</label><br>
<select id="o" name="occasion" required><option value="">– Choose –</option><option>Wedding</option><option>Diwali</option><option>Corporate</option></select></p>
<p><label for="q">Quantity (kg)*</label><br><input id="q" name="qty" type="number" min="5" max="200" step="0.5" required></p>
<p><label for="d">Event date*</label><br><input id="d" name="event_date" type="date" required></p>
<p><label for="t">Notes</label><br><textarea id="t" name="notes" rows="3" maxlength="300"></textarea></p>
</fieldset>
<p><label><input type="checkbox" name="consent" required> I agree to be contacted on WhatsApp</label></p>
<button type="submit">Send enquiry</button>
</form>🏅 आज के Job Skills
Practice tasks
🏋️ Practice – ख़ुद करें
Solution देखें
<form onsubmit="alert('Thanks!'); return false;">
<p><label>Rating <select name="rating" required><option value="">–</option><option>5</option><option>4</option><option>3</option><option>2</option><option>1</option></select></label></p>
<p><label>Comment<br><textarea name="comment" minlength="10" maxlength="300" required rows="3"></textarea></label></p>
<p><label>E-mail <input type="email" name="email" required></label></p>
<button>Send</button>
</form>Solution देखें
<input name="vehicle" pattern="[A-Z]{2}[0-9]{2}[A-Z]{1,2}[0-9]{4}" title="e.g. RJ14AB1234" placeholder="RJ14AB1234">Quick quiz
🧠 Quick Quiz – ख़ुद को जाँचें
Interview corner
💼 Interview Corner
Client-side vs server-side validation?
Client-side (HTML/JS) तुरंत feedback देता है और ग़लत requests घटाता है; server-side सुरक्षा और data की शुद्धता के लिए ज़रूरी है क्योंकि client checks bypass हो सकते हैं।
Form errors को accessible कैसे बनाते हैं?
Labels लें, नियम पहले बताएँ (aria-describedby), field के पास साफ़ text messages दिखाएँ, सिर्फ़ रंग पर निर्भर न रहें, और पहली error पर focus ले जाएँ।
सारांश
- तय choices के लिए select/option/optgroup; suggestions के लिए datalist।
- लंबे text के लिए textarea; group के लिए fieldset + legend।
- required, min/max, step, minlength/maxlength, pattern browser में validate करते हैं।
- Server पर हमेशा दोबारा validate करें।
Day 8: semantic HTML5 layout – header, nav, main, section, article, aside और footer।
