简体   繁体   中英

jQuery form Validation not working?

this is my code for the "contact us" web page. How come it doesn't work with the jquery validator plugin? Did i do something wrong? Please help. Thanks! The validator simply doesn't work. It allows submitting the form even I didn't fill out anything.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
      <script src="http://code.jquery.com/jquery-latest.js"></script>
      <script type="text/javascript" src="http://dev.jquery.com/view/trunk/plugins/validate/jquery.validate.js"></script>

      <script>
      $(document).ready(function(){
        $("#myform").validate();
      });
      </script>
    </head>

    <body bgcolor="#ffffff">
    <div id="contactuscontent">
      <form class="cmxform" id="myform" method="post" action="sendmail.php">
    Your Name: <br />
    <input type="text" name="visitor" size="35"   />
    <br />
    Your Email:<br />
    <input type="email" name="visitormail" size="35"  />
    <br /> <br />
    Subject:<br />
    <input type="text"  name="subject" size="35"  />
    <br /><br />
    Mail Message:
    <br />
    <textarea name="notes" rows="4" cols="40"  ></textarea>
    <br />
    <input type="submit" value="Send Mail" />
    <br />
    </form>
    </div>
    </body>
    </html>

Your code works fine, but you didn't put any validation rule, that's why it does nothing.

EDIT : At least, add a class="required" to your mandatory inputs.

Best Regards.

  <script>
  $(document).ready(function(){
    $("#myform").validate();
  });
  </script>
</head>

<body bgcolor="#ffffff">
<div id="contactuscontent">
  <form class="cmxform" id="myform" method="post" action="sendmail.php">
Your Name: <br />
<input type="text" name="visitor" class="required" size="35"   />
<br />
Your Email:<br />
<input type="email" name="visitormail" class="email required" size="35"  />
<br /> <br />
Subject:<br />
<input type="text"  name="subject" class="required" size="35"  />
<br /><br />
Mail Message:
<br />
<textarea name="notes" rows="4" cols="40" class="required" ></textarea>
<br />
<input type="submit" value="Send Mail" />
<br />
</form>
</div>
</body>
</html>

you just need to add classes that you want on each imput not gonna go into detail but you need to look at web standards for html forms and input as well as good formatting practices

Well, jQuery is awesome. But you can do a JavaScript Form Validation without jQuery also. May refer to this JavaScript Form Validation Tutorial .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM