jquery 验证不会阻止表单提交

发布于 2024-10-02 23:23:53 字数 1614 浏览 0 评论 0原文

我在这里抓狂,试图让 jquery 验证与我的表单很好地配合。

验证似乎不起作用。表单只是将自身提交到页面。我可以简要地看到页面提交之前出现的验证错误消息...

我的代码:

//HTML form
<form id="form_scheduleEvent" name="form_scheduleEvent">
  <label for="name">Name:</label><input class="short" type="text" name="name" id="name" />
  <label for="address">Address:</label><input type="text" name="address" id="address" />
  <label for="phone">Phone:</label><input type="text" name="phone" id="phone" />
  <label for="comments">Comments:</label><textarea name="comments" id="comments" /></textarea>
  <input type="submit" id="submitRequest" value="Add"/>
</form>


//jquery
//Validation rules
$('#form_scheduleEvent').validate({
rules: {
    name : {required: true, maxlength: 45},
    address : {required: true, maxlength: 45},
    phone : "required"
}
});


$('#submitRequest').click(function(){
   $.ajax({
       type: "POST",
       url: "common/ajax_event.php",
       data: formSerialized,
       timeout:3000,
       error:function(){alert('Error');},
       success: function() {alert('It worked!');}
  });

 return false;
});

我尝试更新到 jquery 和 jquery.validation 的最新版本...

任何帮助将不胜感激!谢谢。

更新

//The validation is working, but I can't even get the alert to show....
$('#form_scheduleEvent').validate({
 rules: {
   name : {required: true, maxlength: 45}, 
   address : {required: true, maxlength: 45},
   phone : "required",
   submitHandler: function(){
     alert('test');
   }
 }
});

I'm tearing my hair out here trying to get a jquery validation to play nicely with my form.

The validation does not appear to be working. The form just submits itself to the page. I can briefly see the validation error messages appearing before the page submits...

My code:

//HTML form
<form id="form_scheduleEvent" name="form_scheduleEvent">
  <label for="name">Name:</label><input class="short" type="text" name="name" id="name" />
  <label for="address">Address:</label><input type="text" name="address" id="address" />
  <label for="phone">Phone:</label><input type="text" name="phone" id="phone" />
  <label for="comments">Comments:</label><textarea name="comments" id="comments" /></textarea>
  <input type="submit" id="submitRequest" value="Add"/>
</form>


//jquery
//Validation rules
$('#form_scheduleEvent').validate({
rules: {
    name : {required: true, maxlength: 45},
    address : {required: true, maxlength: 45},
    phone : "required"
}
});


$('#submitRequest').click(function(){
   $.ajax({
       type: "POST",
       url: "common/ajax_event.php",
       data: formSerialized,
       timeout:3000,
       error:function(){alert('Error');},
       success: function() {alert('It worked!');}
  });

 return false;
});

I tried updating to the latest version of both jquery and jquery.validation....

Any help would be appreciated! Thanks.

Update

//The validation is working, but I can't even get the alert to show....
$('#form_scheduleEvent').validate({
 rules: {
   name : {required: true, maxlength: 45}, 
   address : {required: true, maxlength: 45},
   phone : "required",
   submitHandler: function(){
     alert('test');
   }
 }
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

徒留西风 2024-10-09 23:23:54

我同意乔什·里巴科夫的观点
请参阅以下错误: https://github.com/jzaefferer/jquery-validation/issues/ 1212

I agree with Josh Ribakoff
See the bug at: https://github.com/jzaefferer/jquery-validation/issues/1212

攒一口袋星星 2024-10-09 23:23:54
$('#form_scheduleEvent').validate({
 rules: {
   name : {required: true, maxlength: 45}, 
   address : {required: true, maxlength: 45},
   phone : "required"
   },
   submitHandler: function(){
     alert('test');
   }

});

提交处理程序应该超出规则。 :-)

$('#form_scheduleEvent').validate({
 rules: {
   name : {required: true, maxlength: 45}, 
   address : {required: true, maxlength: 45},
   phone : "required"
   },
   submitHandler: function(){
     alert('test');
   }

});

Submit handler should be outside rule. :-)

黑凤梨 2024-10-09 23:23:53

您应该将 AJAX 提交调用移至由验证插件中的提交回调触发:

$('#form_scheduleEvent').validate({
rules: {
    name : {required: true, maxlength: 45},
    address : {required: true, maxlength: 45},
    phone : "required",
    submitHandler: function(form) { DoSubmit(); }
}
});


function DoSubmit(){
   $.ajax({
       type: "POST",
       url: "common/ajax_event.php",
       data: formSerialized,
       timeout:3000,
       error:function(){alert('Error');},
       success: function() {alert('It worked!');}
  }

You should move your AJAX submit call to be fired off by the submit callback in the validate plugin:

$('#form_scheduleEvent').validate({
rules: {
    name : {required: true, maxlength: 45},
    address : {required: true, maxlength: 45},
    phone : "required",
    submitHandler: function(form) { DoSubmit(); }
}
});


function DoSubmit(){
   $.ajax({
       type: "POST",
       url: "common/ajax_event.php",
       data: formSerialized,
       timeout:3000,
       error:function(){alert('Error');},
       success: function() {alert('It worked!');}
  }
饭团 2024-10-09 23:23:53

感谢大家的回复。

我最终将原始代码的 ajax 调用部分包装在以下代码中:

if($('#form_scheduleEvent').valid() == true){
  //Process ajax request...
}
return false;

Thanks everyone for their reply.

I ended up wrapping the ajax call part of my original code in the following code:

if($('#form_scheduleEvent').valid() == true){
  //Process ajax request...
}
return false;
原来是傀儡 2024-10-09 23:23:53

如果您放置的规则没有匹配方法,则可能会发生这种情况,请参阅&为我的错误报告投票。 https://github.com/jzaefferer/jquery-validation/issues/1212

This can happen if you put a rule that doesn't have a matching method, see & vote for my bug report. https://github.com/jzaefferer/jquery-validation/issues/1212

梦幻的味道 2024-10-09 23:23:53

您在验证检查之外调用表单提交。你可能会想要这样的东西:

$('#form_scheduleEvent').validate({
rules: {
    name : {required: true, maxlength: 45},
    address : {required: true, maxlength: 45},
    phone : "required",
    success: function() {
      $.ajax({
        type: "POST",
        url: "common/ajax_event.php",
        data: formSerialized,
        timeout:3000
      }
    }
});

You are calling form submission outside of the validation check. You'll probably want something like this:

$('#form_scheduleEvent').validate({
rules: {
    name : {required: true, maxlength: 45},
    address : {required: true, maxlength: 45},
    phone : "required",
    success: function() {
      $.ajax({
        type: "POST",
        url: "common/ajax_event.php",
        data: formSerialized,
        timeout:3000
      }
    }
});
请帮我爱他 2024-10-09 23:23:53

正如 Grillz 所说,您要分别提交和验证,您应该在点击事件中明确调用验证来执行它。

或者反之亦然,正如帕迪所展示的那样! ;)

As Grillz said you are submitting and validating separately, you should explicitely call validation inside your click event to perform it.

Or viceversa as Paddy shows! ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文