验证后提交表单

问题描述:

我有以下表格:

<form id="form" action="comments.php" method="post">
  <ul>
    <li><div class="formbox_titles">Name</div><input type="text" name="name" class="required comm_input" /></li>
    <li><div class="formbox_titles">Email</div><input type="text" name="email" class="required comm_input"/></li>
    <li><div class="formbox_titles">Message</div><textarea name="message" class="required comm_text"></textarea></li>
    <li><input type="submit" value="Submit"></li>
  </ul>
</form>

并遵循JQuery提交表单:

and following JQuery for form submission:

target: '#preview', 
  success: function() { 
  $('#formbox').slideUp('fast'); 
}

现在的问题是我也有表单验证JQuery代码

Now the problem is that I also have form validation JQuery code

$().ready(function() {
    // validate comment form
    $("#form").validate({ 
    });
});

它们两个都很好用,并且表单会弹出警告,警告所有三个字段不能为空,但是表单中的日期无论如何都会提交到数据库中.我正在使用以下代码进行表单验证 http://bassistance.de/jquery-plugins/jquery -plugin-validation/问题是如何在第二个内部添加第一个jquery,因此一旦填写了必填字段,第一个将被执行?

Both of them work great and form does popup warning that all three fields cant be empty but the date inside form is submitted into database anyway. Im using following code for form validation http://bassistance.de/jquery-plugins/jquery-plugin-validation/ So the question is how to add first jquery inside second, so first will be executed once the required fields are filled?

希望有人会帮助您.谢谢.

Hope someone will help. Thanks.

在您的示例中,我不清楚页面的控制流程,但是,我假设您正在某个地方调用submit()方法.在验证表单之后,在提交表单之前,请使用valid()方法检查它的有效性.您的代码应如下所示:

From your example, I am not clear about the control flow of your page, however, I am assuming you are calling the submit() method somewhere. After you have validated the form and before you submit it, check it's validation with the valid() method. Your code should look something like this:

$("#form").validate();
if ($('#form').valid())
    $('#form').submit();