jQuery验证一条规则失败不应跳到另一条规则 [英] Jquery validation one rule fails should not jump to another rule

查看:62
本文介绍了jQuery验证一条规则失败不应跳到另一条规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jquery验证,因此我必须实现功能,因此,如果任何规则失败,则不应提交表单.验证工作正常进行.

I am using jquery validation, I have to implement functionality so that if any rule fails the form should not submit. The work is done validation working fine.

问题是,如果多个规则失败,则会在每个文本框中显示错误消息,该错误会传播到所有规则.

The problem is if multiple rules are failed it is showing error messages in each text box, the failure is propagating to all rules.

我想实现一种功能,如果一条规则失败,它应该仅在该元素的错误消息中停在那里.

I want to achieve a functionality in which if one rule fails it should stop there with error message of that element only.

<form id="myform">
 <label for="firstName">FirstName</label>
 <input class="left" id="fname" name="fname" />
 <br>
 <label for="lastName">LastName</label>
 <input class="left" id="lname" name="lname" />
 <br>
 <label for="fromDate">fromDate</label>
 <input class="left" id="fromDate" name="fromDate" />
 <br>
 <label for="endDate">endDate</label>
 <input class="left" id="endDate" name="endDate" />
 <br>
 <input type="submit" value="Validate!">
 </form>

规则:

$( "#myform" ).validate({
  rules: {
    fname:"required",
    lname:"required",
    fromDate:"required",
    endDate:"required"
  }
});

这是我的问题msg的演示-第一个文本框"应该出现一次此字段必填".错误不应传播到其余文本框.

This is demonstration of my problem msg-"This field is required" should appear once i.e. for first text box. Error shouldn't propagate to remaining text boxes.

如果有人能理解我的要求,请回复.(这是我的演示代码.)

Please respond if any body understands what I want.(This is my demo code.)

推荐答案

您可以使用自定义错误处理程序 showErrors ,仅在错误列表中设置第一个发现的错误:

You can use the custom error handler showErrors, where you only set the first found error in the error list:

showErrors: function(errorMap, errorList) {
    if (errorList.length) {
       this.errorList = [errorList[0]];
    }

    this.defaultShowErrors();
  }

错误列表必须是一个数组,所以这就是第一个错误放在定义数组的[]内的原因.

The error list needs to be an array, so that's why the first error is placed inside the [], which defines an array.

因此,您总共得到:

$("#myform").validate({
  rules: {
    fname: "required",
    lname: "required",
    fromDate: "required",
    endDate: "required"
  },
  showErrors: function(errorMap, errorList) {
    if (errorList.length) {
       this.errorList = [errorList[0]];
    }

    this.defaultShowErrors();
  }
});

JsFiddle

JsFiddle

现在,如果您希望内部验证检查在其第一个错误时停止.因此,不仅在视觉上不显示错误,而且一旦一个字段存在错误,则实际上停止检查.您可以覆盖内部检查功能checkForm().

Now, if you want the internal validation check stop at its first error. So not only visually not showing the errors, but actually stopping checking once one field is falsy. You can overwrite the internal check function checkForm().

一旦我知道至少一个字段是虚假的,我将简单地检查元素是true还是false.所以我打破了检查循环:

I will simply check if the element is true or false, once false I know at least one field is falsy. So I break the check loop:

validator.checkForm = function() {
  this.prepareForm();
  for (var i = 0, elements = (this.currentElements = this.elements()); elements[i]; i++) {
    if (!this.check(elements[i]))
      return false;
  }
  return this.valid();
};

JsFiddle

现在,我想提醒您,我不是此插件的创建者,因此,我不完全了解此插件是否可以正常工作.我建议您编写自己的验证代码,以完全控制自己的工作.

Now I want to mind you I am not the creator of this plugin, so I do not fully know if this works accordingly. I would suggest to write your own validation code, to have full control on what you are doing.

这篇关于jQuery验证一条规则失败不应跳到另一条规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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