← Back to HTML Course | Chapter 10: Reference & Interview Prep | Lesson 8 of 20

HTML इनपुट वैलिडेशन

HTML5 आपको बिना कोई JavaScript लिखे built-in form validation देता है। आप fields को required बना सकते हैं, length limits सेट कर सकते हैं, patterns define कर सकते हैं, और number ranges restrict कर सकते हैं — यह सब सिर्फ HTML attributes के ज़रिए ही होता है। error messages browser खुद-ब-खुद handle करता है।
Syntax
markup
<input type="type" required minlength="number" maxlength="number" pattern="regex">
<input type="number" min="number" max="number">

Required और Length वैलिडेशन

required attribute किसी field के खाली होने पर form submission रोकता है। minlength और maxlength यह control करते हैं कि input कितना छोटा या लंबा हो सकता है। ये built-in checks form को server पर भेजे जाने से पहले पूरी तरह browser में ही चलते हैं।

Note:
  • required को हमेशा एक स्पष्ट placeholder के साथ जोड़ें जो expected format दिखाए।
  • Accepted attributes:
  • required — boolean
  • minlength — integer
  • maxlength — integer
Warning: Browser validation को आसानी से bypass किया जा सकता है — अपने server पर हमेशा फिर से validate करें।

उदाहरण: Required and Length Validation

markup
<input type="text" required minlength="3" maxlength="10">

पैटर्न वैलिडेशन

pattern attribute एक regular expression accept करता है जिससे input value को match करना ज़रूरी होता है। यह आपको postal codes, product IDs, या usernames जैसे custom formats validate करने देता है।

चूँकि pattern सिर्फ structure चेक करता है, आपको server पर भी यही rule फिर से validate करनी चाहिए क्योंकि client-side checks को bypass किया जा सकता है।

Note:
  • हमेशा एक title attribute जोड़ें जो required format समझाए — browsers इसे error message के रूप में दिखाते हैं।
  • Accepted attributes:
  • pattern — regular expression (full value match)
  • title — message describing the pattern
Warning: pattern में regular expressions अपने आप anchored होते हैं — शुरुआत और अंत में आपको ^ और $ की ज़रूरत नहीं।

उदाहरण: Pattern Validation

markup
<input type="text" pattern="[0-9]{5}" title="Enter a 5-digit postal code">

नंबर रेंज वैलिडेशन

number inputs के लिए, min और max allowed range तय करते हैं। step attribute values के बीच allowed increment control करता है। min, max, और step सेट होते ही browsers अपने आप एक छोटा increment/decrement arrow UI दिखाते हैं।

Note:
  • decimal values की अनुमति देने के लिए currency inputs में step=0.01 इस्तेमाल करें।
  • Accepted attributes:
  • min — lower bound
  • max — upper bound
  • step — number | any
Warning: min और max attributes सिर्फ number, range, date, और time input types पर काम करते हैं।

उदाहरण: Number Range Validation

markup
<input type="number" min="1" max="10" step="1">

Email और URL टाइप वैलिडेशन

किसी input का type email या url सेट करने से browser बिना कोई custom validation logic लिखे यह अपने आप चेक कर लेता है कि दी गई value एक valid email address या web address जैसी दिखती है, form submit होने से पहले।

Note:
  • Accepted attributes:
  • type — email | url
  • multiple — boolean (email only)
  • required / pattern / minlength / maxlength

उदाहरण: Email and URL Type Validation

markup
<input type="email" required>
<input type="url" required>

कस्टम वैलिडेशन मैसेजेस

setCustomValidity method, pattern के साथ दिखने वाले title attribute के साथ मिलकर, आपको browser के generic 'Please match the requested format' message की जगह ऐसा wording देने देता है जो असल में यह समझाए कि user को क्या ठीक करना है।

उदाहरण: Custom Validation Messages

markup
<input type="text" oninvalid="this.setCustomValidity('Please enter your full name')">
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. #}

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.