← Back to jQuery Course | Chapter 8: Working with Forms | Lesson 4 of 5

Client-Side Validation

Required checks users को तुरंत feedback देते हैं।
Syntax
javascript
$(form).submit(function (event) {
  if (/* invalid */) {
    event.preventDefault();
  }
});

Required Fields

Submission की अनुमति देने से पहले यह check करना कि required fields खाली नहीं हैं, user को immediate, उपयोगी feedback देता है बजाय server तक एक incomplete form पहुँचने देने के।

उदाहरण: Required Fields

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <input id="name" type="text">
    <script>
      if ($("#name").val().trim() === "") {
      console.log("Name is required");
      }
    </script>
  </body>
</html>

Email Validation

एक simple regular expression किसी email address का basic shape check कर सकती है -- कुछ text, एक @ symbol, और एक domain जैसा -- हालाँकि यह confirm नहीं कर सकता कि address असल में exist करता है या नहीं।

उदाहरण: Email Validation

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <input id="email" type="text" value="not-an-email">
    <script>
      const pattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
      console.log(pattern.test($("#email").val()));
    </script>
  </body>
</html>

Length Validation

Length checks passwords और messages जैसे fields के लिए उपयोगी हैं जिन्हें characters की minimum या maximum संख्या चाहिए। val().length पढ़ना आपको उसे किसी सीमा से compare करने देता है।

उदाहरण: Length Validation

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <input id="password" type="text" value="abc">
    <script>
      if ($("#password").val().length < 8) {
      console.log("Password too short");
      }
    </script>
  </body>
</html>

Validation Messages

Clear, specific validation messages -- exactly बताते हुए क्या गलत है और कहाँ -- forms को top पर एक single generic error से समझने में आसान बनाते हैं।

उदाहरण: Validation Messages

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <input id="email" type="text">
    <span id="emailError"></span>
    <script>
      $("#emailError").text("Please enter a valid email address");
    </script>
  </body>
</html>

Complete Validation

Required-field, format, और length checks को एक validation flow में combine करना आपको submission से पहले हर problem पकड़ने और उन्हें एक साथ report करने देता है।

उदाहरण: Complete Validation

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <input id="name" type="text">
    <input id="email" type="text" value="bad">
    <script>
      const errors = [];
      if ($("#name").val().trim() === "") errors.push("Name required");
      if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test($("#email").val())) errors.push("Invalid email");
      console.log(errors);
    </script>
  </body>
</html>
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. #}
आम गलतियां
  1. सिर्फ $("#name").val() === "" check करना और सिर्फ spaces वाले input को miss कर देना; whitespace को filled न गिनने के लिए पहले .trim() इस्तेमाल करें।
  2. एक email regex लिखना जो बहुत strict या बहुत loose हो और उसे इस बात का proof मानना कि address exist करता है, जबकि client-side checks सिर्फ format test करते हैं।
  3. अकेले client-side validation पर भरोसा करना, यह भूलते हुए कि users इसे bypass कर सकते हैं, इसलिए server को दोबारा validate करना ज़रूरी है।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 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.