Is it possible to validate the form without submitting it using the JQuery validation plugin?
I use a button.click function for validating, the button isn't a submit input.
The jquery validate plugin requires a form element to function, so you should have your form fields (no matter how few) contained inside a form. You can tell the validation plugin not to operate on form submission, then manually validate the form when the correct submit button is clicked.
You need to do two things if validation fails, e. preventDefault() and to return false. This really works.
You can only disable it after the form has successfully passed validation. Use the plugin's submitHandler option for this as it's fired on a button click only when the form has passed validation. $(). ready(function() {... is not recommended as per jQuery documentation.
Yes, it is possible to test the form's validity without submitting it.
See .valid()
method.
Whenever .valid()
is called the form is tested, and the method will also return a boolean.
$(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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With