触发事件是否有验证错误? [英] Trigger an event if there are validation errors?

查看:158
本文介绍了触发事件是否有验证错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个手柄添加到一个表单验证失败,任何情况下。

我读过<一个href=\"http://stackoverflow.com/questions/4747017/how-to-add-a-submithandler-function-when-using-jquery-unobtrusive-validation\">this,解释,我要添加一个处理程序如下:

  $('形式')。绑定(无效-form.validate',函数(){
  的console.log('形式是无效的!');
});

但这个事件仅触发,当我尝试提交表单。

我需要处理的是解雇任何时候的形式进行验证后一个事件(即元素失去焦点等)。

我想要实现的是,我有一个大的形式(〜50场),并且它在的引导标签的。结果
我想,那个时候有任何新的验证失败或成功,设置或包含无效/有效元素标签的分页标题清除错误类。附:我的问题是不如何在选项卡中设置这些类。我只是想知道,以处理在整个形式每个验证状态变化被通知什么事件(S)。


解决方案

  

我需要一个处理程序添加到表单验证失败,任何情况下。


  $('输入,文本区域,选择')。在('事件的内容KEYUP',函数(){
   如果($('形式')。有效的()){
       // 做一点事
   }
});



  

我想,那个时候有任何验证错误,在包含无效元素标签标题设置错误类。


嗯,这比您最初询问有点不同。的(忽略我的答案上半年)

使用高亮 unhighlight 与jQuery的DOM遍历设置和取消任何类中的DOM你一起选择希望。 高亮火灾时,一些是无效的, unhighlight 火灾时,它变得有效。请参见这个答案作为一个例子。

使用 .setDefaults()方法设置这些选项,因为你使用不显眼的验证插件并没有直接进入 .validate()方法。

I need to add an handle to any circumstance where a form validation fails.

I've read this, that explains that I have to add a handler as follows:

$('form').bind('invalid-form.validate', function () {
  console.log('form is invalid!');
});

But this event only fires when I try to submit the form.

I need to handle an event that's fired ANY time the form is post-validated (i.e. element loses focus etc.).

What I'm trying to achieve is, I have a large form (~50 fields), and it's splitted in Bootstrap tabs.
I want, that when there is any new validation failure or success, set or clear an error class in the tab title of the tab that contains the invalid/valid elements. P.S. my question is not on how to set those classes in the tabs. I only want to know what event(s) to handle in order to be notified upon each validation state change in the entire form.

解决方案

I need to add a handler to any circumstance where form validation fails.

$('input, textarea, select').on('focusout keyup', function() {
   if ($('form').valid()) {
       // do something
   }
});


I want, that when there is any validation error, set an error class in the tab title that contains the invalid elements.

Well, that's a bit different than what you originally asked about. (Ignore the first half of my answer.)

Use the highlight and unhighlight options along with jQuery DOM traversal to set and unset any class in the DOM you wish. highlight fires when something is invalid and unhighlight fires when it becomes valid. See this answer as an example.

Use the .setDefaults() method to set these options since you're using the unobtrusive-validation plugin and have no direct access to the .validate() method.

这篇关于触发事件是否有验证错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆