在不提交表单的情况下调用 JQuery Validate Plugin
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16725414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call JQuery Validate Plugin without submitting the form
提问by lady_OC
Is it possible to validate the form without submitting it using the JQuery validation plugin?
是否可以在不使用 JQuery 验证插件提交的情况下验证表单?
I use a button.click function for validating, the button isn't a submit input.
我使用 button.click 函数进行验证,该按钮不是提交输入。
回答by Sparky
Yes, it is possible to test the form's validity without submitting it.
是的,可以在不提交表格的情况下测试表格的有效性。
Whenever .valid()
is called the form is tested, and the method will also return a boolean.
每当.valid()
调用该表单时,都会对其进行测试,并且该方法还将返回一个布尔值。
$(document).ready(function () {
$('#myform').validate({ // initialize the plugin
// rules & options
});
$('#button').click(function() {
if ($('#myform').valid()) {
alert('form is valid - not submitted');
} else {
alert('form is not valid');
}
});
});
Working DEMO: http://jsfiddle.net/zMYVq/
工作演示:http: //jsfiddle.net/zMYVq/