← Back to HTML Course | Chapter 5: Forms | Lesson 5 of 7

HTML Input Attributes

कल्पना कीजिए कि आप किसी सोडा वेंडिंग मशीन के पास जाते हैं। कुछ बटनों के नीचे प्राइस लिखा होता है (placeholders), कुछ पूरी तरह से आउट-ऑफ-ऑर्डर लॉक होते हैं (disabled), और कुछ के लिए काम करने से पहले आपको एकदम सही सिक्के डालने होते हैं (required)। HTML input attributes वही मशीन सेटिंग्स हैं। ये आपके पेज पर दिखने वाले फील्ड के टाइप को नहीं बदलते, बल्कि यह कंट्रोल करते हैं कि वह फील्ड कैसे बर्ताव करता है, कौन सा डेटा एक्सेप्ट करता है, और यूज़र्स उसके साथ कैसे इंटरैक्ट करते हैं। इन attributes को कॉन्फ़िगर करना यूज़र्स को आपके forms में गाइड करने और input errors रोकने के लिए बेहद ज़रूरी है।
Syntax
markup
<input type="type" name="name" value="value" placeholder="hint" required>

value और placeholder Attributes

value attribute पेज लोड होने पर input फील्ड के अंदर डिफ़ॉल्ट, पहले से भरा हुआ टेक्स्ट सेट करता है।

placeholder attribute फील्ड के अंदर एक हल्का, अस्थायी संकेत दिखाता है जो यूज़र को बताता है कि क्या टाइप करना है, और यूज़र के टेक्स्ट दर्ज करना शुरू करते ही यह गायब हो जाता है।

Note:
  • फॉर्मेटिंग उदाहरण दिखाने के लिए placeholders का उपयोग करें, जैसे email फील्ड्स पर '[email protected]'।
  • Accepted attributes:
  • value — initial or submitted value
  • placeholder — hint text shown while empty
  • Placeholder does not replace a label
Warning: असली label elements की जगह कभी भी placeholders इस्तेमाल न करें, क्योंकि screen readers उन्हें स्किप कर सकते हैं।

उदाहरण: The value and placeholder Attributes

markup
<input type="text" value="Default text">
<input type="email" placeholder="[email protected]">

readonly और disabled Attributes

readonly attribute यूज़र को फील्ड के अंदर टेक्स्ट देखने और सिलेक्ट करने देता है, लेकिन उन्हें उसे एडिट करने से रोकता है, फिर भी सबमिशन के समय डेटा सर्वर पर भेजा जाता है।

disabled attribute एलिमेंट को पूरी तरह लॉक कर देता है, उसे ग्रे-आउट कर देता है और उसका डेटा सबमिट होने से रोकता है।

Note:
  • उस डेटा (जैसे order numbers) को दिखाने के लिए readonly attribute का उपयोग करें, जिसे आप यूज़र से कॉपी करवाना चाहते हैं लेकिन बदलवाना नहीं चाहते।
  • Accepted attributes:
  • readonly — boolean; value is submitted, not editable
  • disabled — boolean; not editable, not submitted, not focusable
Warning: Disabled input fields को form submission के दौरान पूरी तरह इग्नोर किया जाता है, इसलिए उनका डेटा सर्वर पर नहीं भेजा जाता।

उदाहरण: The readonly and disabled Attributes

markup
<input type="text" value="Order #1234" readonly>
<input type="text" value="Cannot edit" disabled>

required और autofocus Attributes

required attribute ब्राउज़र को बताता है कि फॉर्म सबमिट होने से पहले उस फील्ड को ज़रूर भरना चाहिए, और खाली छोड़ने पर एक native error message दिखाता है।

autofocus attribute पेज लोड होते ही अपने आप कर्सर को किसी specific फील्ड के अंदर रख देता है।

Note:
  • पेज नेविगेशन को सहज रखने के लिए प्रति पेज सिर्फ एक ही input फील्ड पर autofocus का उपयोग करें।
  • Accepted attributes:
  • required — boolean; must be filled before submit
  • autofocus — boolean; focus on page load (one per page)
Warning: पेज में नीचे स्थित fields पर autofocus लगाने से ब्राउज़र लोड होते ही आपके header content को छोड़कर आगे जंप कर सकता है।

उदाहरण: The required and autofocus Attributes

markup
<input type="text" required autofocus>

minlength और maxlength से Character Limits

minlength attribute किसी फील्ड में यूज़र द्वारा टाइप किए जाने वाले कैरेक्टर की न्यूनतम संख्या तय करता है, और maxlength attribute अधिकतम सीमा तय करता है, जिससे यूज़र उस सीमा के बाद कोई कैरेक्टर दर्ज नहीं कर पाता।

Note:
  • सुरक्षित, मानक कैरेक्टर काउंट लागू करने के लिए passcode फील्ड्स पर maxlength का उपयोग करें।
  • Accepted attributes:
  • minlength — non-negative integer
  • maxlength — non-negative integer
  • Apply to text, search, url, tel, email, password and textarea
Warning: data truncation errors रोकने के लिए सुनिश्चित करें कि आपकी character limits आपकी database settings से मेल खाती हों।

उदाहरण: Character Limits with minlength and maxlength

markup
<input type="text" minlength="4" maxlength="8">

pattern और title से Custom Validation

pattern attribute आपको Regular Expressions (regex) का उपयोग करके कस्टम validation rules लिखने देता है, ताकि यह जांचा जा सके कि यूज़र इनपुट किसी विशेष फॉर्मेटिंग स्ट्रक्चर (जैसे zip codes या phone numbers) से मेल खाता है या नहीं, जबकि title attribute वैलिडेशन फेल होने पर एक कस्टम error tooltip दिखाता है।

Note:
  • जटिल JavaScript स्क्रिप्ट्स की ज़रूरत के बिना सुरक्षित, कस्टम इनपुट फॉर्मेट लागू करने के लिए pattern attribute का उपयोग करें।
  • Accepted attributes:
  • pattern — regular expression matched against the full value
  • title — text explaining the required format
  • Applies to text, search, url, tel, email, password
Warning: यह समझाने के लिए कि किस formatting error की वजह से validation फेल हुआ, हमेशा स्पष्ट title tooltips लिखें।

उदाहरण: Custom Validation with pattern and title

markup
<input type="text" pattern="[0-9]{5}" title="Enter a 5-digit zip code">
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.