简体   繁体   English

jQuery表单验证不起作用?

[英]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? jQuery验证程序插件为何不起作用? 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. 编辑 :至少,将class="required"到您的强制输入中。

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 您只需要在每个输入上添加所需的类,就不必对其进行详细介绍,但是您需要查看html表单和输入的网络标准以及良好的格式化习惯

Well, jQuery is awesome. 好吧,jQuery非常棒。 But you can do a JavaScript Form Validation without jQuery also. 但是您也可以在没有jQuery的情况下执行JavaScript表单验证。 May refer to this JavaScript Form Validation Tutorial . 可以参考此JavaScript表单验证教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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