使用 bassistance 的 jQuery 验证插件,如何以编程方式验证表单而不触发错误消息?
问题有点复杂。让我用最终目标来回答这个问题。
目标
我想运行某种代码,使用验证插件检查表单的有效性,但不会触发错误消息。我希望表单的提交操作遵循相同的规则,但随后抛出错误。
基本上,我想禁用“提交”按钮,直到表单完全有效。因此,每当字段发生更改时,我都需要检查是否满足所有规则。此过程会触发显示错误。这(就我而言)是有问题的。
问题
有谁知道使用验证插件检查表单有效性的方法,以便在表单上的任何内容发生更改时仅返回布尔值 true 或 false,而不会触发错误消息?
我尝试过的
我尝试过使用 success
方法,但是这只在提交表单后才起作用,并且它只直接影响更改的字段,而不是整个表单。
我还尝试使用 $('#start_date').valid()
,但是正如我上面所说,这会触发错误显示在表单上。
有什么想法吗?
插件(这样就不会混淆):bassistance 的 jQuery 验证
The question is a bit complicated. Let me preface this question with the end goal.
Goal
I would like to run some kind of code that will check the validity of the form using the validate plug-in but not trigger the errors messages. I would like the Submit action of the form to follow the same rules, but then throw the errors.
Basically, I want to disable the Submit button until the form is completely valid. Therefore, anytime a field is changed, I need to check to see all the rules are satisfied. This process triggers the errors to be shown. This (in my case) is problematic.
Question
Does anyone know of a way to check the validity of the form using the Validation plug-in to just return a boolean true or false without triggering the errors messages any time anything on the form is changed?
What I've Tried
I have tried using the success
method, however this only works after the form has been submitted and it only directly affects the field that was changed, not the form as a whole.
I've also tried to use $('#start_date').valid()
, however as I have said above, this triggers the errors to display on the form.
Any ideas?
The plug-in (so there's no confusion): bassistance's jQuery Validation
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我能想到的最快方法是使用自定义验证错误消息放置函数,如果您试图避免触发错误,则该函数不会执行任何操作。因此,
当您想要验证时,只需将
hideerrors
设置为 true,调用validate
,然后再次将hideerrors
设置为 false。The quickest way I could think of would be to use a custom validation error message placement function that did nothing if you were trying to avoid triggering the errors. So something like:
Then when you want to validate you can just set
hideerrors
to true, callvalidate
, then sethideerrors
to false again.昨晚我深入研究了该插件的实际源代码,实际上发现了一个未记录的方法,它正是我想要的!
$('#formname').validate().checkForm()
- 根据表单是否有效返回 true 或 false,而不触发错误消息。我不知道为什么这个方法没有记录,但它确实应该记录。
我的禁用或启用提交按钮的实现是这样的:
I dove into the actual source code of the plug-in last night and actually found an undocumented method that does exactly what I want!
$('#formname').validate().checkForm()
- Returns true or false based on whether or not the form is valid, without triggering the error messages.I don't know why this method isn't documented, but it really should be.
My implementation of it to disable or enable the Submit button is working like this: