Validate fields using JQuery validation without any submit?
Firstly, and most importantly, you must wrap your input elements inside <form></form>
tags for the jQuery Validate plugin to operate. However, a submit button is not required.
Secondly, you can programatically trigger the validity test of any or all elements without a submit button by using the .valid()
method.
$(document).ready(function() {
$('#myform').validate({ // initialize the plugin on your form.
// rules, options, and/or callback functions
});
// trigger validity test of any element using the .valid() method.
$('#myelement').valid();
// trigger validity test of the entire form using the .valid() method.
$('#myform').valid();
// the .valid() method also returns a boolean...
if ($('#myform').valid()) {
// something to do if form is valid
}
});
DEMO: http://jsfiddle.net/URQGG/
You will have to wrap your fields within a form to use the validation plugin and it's a good practice anyway. Also, you can invoke the plugin's validation programmatically and check if the form is valid by doing:
var $form = $('#your_form'),
validator = $form.validate({...});
//validate the form
validator.form();
//check if the form is valid
if ($form.valid()) {
//form is valid
}
For more options, have a look at the docs.