jQuery 验证插件:如何强制验证以前有效的字段?
我正在使用 jQuery 验证插件来验证表单。您知道如何对以前成功的字段强制重新验证吗?
我已使用以下检查尝试了 .form 函数(这是在用户单击“提交”后执行的):
if ($('#form1').validate().form()==false)
{
formValid = false;
}
但是,上面的代码似乎不会重试验证,因此已经成功验证的字段(即旁边有勾号) ) 不再检查。
想要对以前成功的字段重试重新验证的原因是它们依赖于远程验证,并且结果(成功或失败)可能会在用户离开字段和单击提交之间发生变化。 (这适用于“用户名”字段)。
如果它影响答案,我有多个表单需要验证(为简单起见,在上面的代码片段中我仅引用“#form1”)。
预先感谢您的任何建议,
罗布
I'm using jQuery Validation plugin to validate a form. Do you know how to force revalidation on previously successful fields?
I have tried the .form function using the following check (this is performed after the user clicks 'submit'):
if ($('#form1').validate().form()==false)
{
formValid = false;
}
However, it appears that the code above does not retry validation so fields that are already successfully validated (ie have tick next to them) are not checked again.
The reason for wanting to retry revalidation on previously successful fields is that they rely on remote validation, and the outcome (success or failure) can change between the user leaving the field, and clicking submit. (This applies to a 'username' field).
In case it affects the answer I have multiple forms to validate (for simplicity, in the code snippet above I refer to '#form1' only).
Thanks in advance for any advice,
Rob
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
远程字段的验证状态通过
$.data()
元素存储你想验证,所以你可以使用.removeData()
来清除它......所以它被迫重新验证:这会强制检查值是否已更改(我们需要重新验证)为
true
:如果只有特定字段需要重新验证,就像您所说的用户名,您可能希望将
$("#form1 :input")
选择器缩小到仅您想要的字段,以使其更加高效。The state of validation for remote fields is stored via
$.data()
with the element you want to validate, so you could use.removeData()
to clear that out...so it's forced to revalidate:This forces the check if the value has changed (we need to re-validate) to be
true
:If there are only specific fields that need re-validating, like you said username, you may want to narrow the
$("#form1 :input")
selector to only the fields you want, to make it a bit more efficient.当我研究使用 jQuery 远程非侵入式验证时,我发现了这个答案。这不是世界上记录最好的主题,而且也并非没有怪癖。如此之多以至于我最终写了一篇关于我所学到的知识的博客文章。链接此处以防有帮助:
http://icanmakethiswork.blogspot.com /2012/03/jquery-unobtrusive-remote-validation.html
I came upon this answer when I was looking into using jQuery remote unobtrusive validation. It wasn't the best documented topic in the world and also it wasn't without quirks. So much so that I ended up writing a blog post about what I learned. Link here in case helpful:
http://icanmakethiswork.blogspot.com/2012/03/jquery-unobtrusive-remote-validation.html