I am trying to validate but not submit multiple forms with one button using the jquery validation plugin. Here is a simplified version of my code
<script>
$(document).ready(function() {
var validator1 = $("#form1").validate({
rules: {
field1: {required: true}
},
messages:{
field1:{required:"Field 1 required"},
submitHandler: function(form) {
//some code
}
});
var validator2 = $("#form2").validate({
rules: {
field2: {required: true}
},
messages:{
field2:{required:"Field 2 required"},
submitHandler: function(form) {
//some code
}
});
});
function submit_forms() {
$('#form1').validate();
$('#form2').validate();
if ($('#form1').valid() && $('#form2').valid()) {
alert('both forms are valid');
//do some stuff
}
}
</script>
<form id="form1>
<input type="text id="field1" />
</form>
<form id="form2">
<input type="text id="field2" />
</form>
<input type="button" onclick="submit_forms();" />
However it isn;t working. I want both forms to be validated and any validation messages to be displayed. I do not want the submit handlers of either form to be called. I want to decide what to do in the click event of the button. My code isn't working at all as the validation messages are not being displayed if there are errors and the alert isnt being displayed if the code is fine.
I'm not a javascript programmer so I don't really know what I am doing. Do i need to call the validate methods in the button click function or have they already been called by this code in document ready: $("#form1").validate(....
I also didnt think the submit handler should be called because I am not submitting the form anywhere, only calling the validate and valid methods. Do these methods call submit() behind the scenes?
Thanks in advance for your help