I have two forms on one page with a starting and closing <form></form> tag for each form. I m validating it with JQuery Validate Plugin. I have to validate both forms with one single button. The problem is that validation of both forms required a separate button for each form which is not required. the code is given below:-
$(document).ready(function(){
$('.formValidate').each(function() { // <- selects every <form> on page
$(this).validate({
rules: {
orderno: { //placed at first form
required: true,
minlength: 5
},customername: { //placed at second form
required: true,
minlength: 5
}
},
//For custom messages
messages: {
order:{
required: "Enter a Order No",
minlength: "Enter at least 5 characters"
},customername:{
required: "Enter a username",
minlength: "Enter at least 5 characters"
}
},
errorElement : 'div',
errorPlacement: function(error, element) {
var placement = $(element).data('error');
if (placement) {
$(placement).append(error)
} else {
error.insertAfter(element);
}
},
submitHandler: function() {
console.log("Order submitted!");
}
});
});
}); //end readyfunction
HTML is as given below:-
<form class="formValidate" id="tstordform" method="get" action="">
<div class="row">
<div class="input-field col s12">
<i class="mdi-image-tag-faces prefix"></i>
<input id="orderno" name="orderno" type="text" data-error=".errorTxt1">
<label for="orderno">Order No.*</label>
<div class="errorTxt1"></div>
</div>
</div>
</form>
<form class="formValidate" id="cstdataform" method="get" action="">
<div class="row">
<div class="input-field col s12">
<i class="mdi-action-account-circle prefix"></i>
<input id="customername" name="customername" type="text" data-error=".errorTxt6">
<label for="customername">Customer Name</label>
<div class="errorTxt6"></div>
</div>
<div class="input-field col s3">
<button id="validateboth" class="btn waves-effect waves-light center cyan submit" type="submit" name="action">Save Order
<i class="mdi-content-send right"></i>
</button>
</div>
</div>
</form>
Kindly help me!
$(form).each(function() {