问题描述
我有一个文本框.我不希望在文本框中输入无效值后单击提交按钮来进行模糊验证.
I have a text box. I do not want validation on blur to occur if after entering an invalid value in a text box I click a submit button.
如果我模糊处理其他任何控件-那么我要做希望进行验证.
If I blur to any other control - then I do want validation to occur.
在这种精确情况下,Windows [desktop]窗体允许将控件指定为"no-causing-validation
".
Windows [desktop] forms allowed to designate a control as "no-causing-validation
" for this precise situation.
是否可以在jquery validate中将控件指定为"no-cause-validation
"?
Is it possible to designate a control as "no-cause-validation
" in jquery validate?
推荐答案
是可以的,但是您必须以代码而不是以声明的方式来实现.
Yes it is possible, but you have to do it in code rather than in a declarative way.
要执行此操作,您必须覆盖事件的"on"处理程序-在这种情况下为模糊(对焦)
To do it you have to override the 'on' handler for the event - in this case blur (focusout)
这是jsfiddle上的示例,请验证这样的代码
$("form").validate({
onfocusout: function (element, event) {
if (element.id === "fname") {
this.element(element);
}
}
});
使用这样的html
<form>
<label for="fname">First Name</label>
<input name="fname" id="fname" class="required number">
<label for="sname">Last Name</label>
<input name="sname" id="sname" class="required number">
<input type="submit" />
</form>
这是一个基本示例,可以在模糊时验证一个字段,而不验证另一个字段.您可以通过在要验证的字段中检查属性来进一步进行此操作,但是通常的方法是
It is a basic example that validates one field on blur and not the other field. You could take this further by checking for attributes on the fields you want to validate, but the general approach is
- 使用函数覆盖onfocousout(或onkeyup等)
- 在该函数内,对于要验证的字段,请调用element(...)
此处讨论- https://github.com/jzaefferer/jquery-validation /issues/564
这篇关于jQuery验证:如果焦点丢失到提交按钮,如何防止模糊(聚焦)验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!