I'm using the jquery validation plugin along with this other one I found to create a sort of form wizard. The problem I'm having is making the validation plugin fire on the next buttons for the form wizard so each step process gets validated instead of just being on submit button.
Here is the jsfiddle: http://jsfiddle.net/DHNPz/
The javascript part contains the code I wrote for the form and also the formtowizard JS. I am assuming I need to edit this part of the code:
function createNextButton(i) {
var stepName = "step" + i;
$("#" + stepName + "commands").append("<a href='#' id='" + stepName + "Next'><button class='next' type='button'>Next Step</button></a>");
$("#" + stepName + "Next").bind("click", function(e) {
$("#" + stepName).hide();
$("#step" + (i + 1)).show();
if (i + 2 == count)
$(submmitButtonName).show();
selectStep(i + 1);
});
}
Inside of the click function. I'm just not sure how to call the validation trigger here
Please help!
First, you'll need to add something like
ignore: ':hidden'
to your validate
options so that it only validates visible fields. That way you can validate only the fields visible in each step, moving to the next step once they are valid. You can then check the validation at any time by running
$('#RMAForm').validate().form()
to trigger the validation when someone clicks the next button. You'd update the code you pasted above like so:
function createNextButton(i) {
var stepName = "step" + i;
$("#" + stepName + "commands").append("<a href='#' id='" + stepName + "Next'><button class='next' type='button'>Next Step</button></a>");
$("#" + stepName + "Next").bind("click", function(e) {
// run the validation and check if the form is valid
if ($('#RMAForm').validate().form()) {
$("#" + stepName).hide();
$("#step" + (i + 1)).show();
if (i + 2 == count)
$(submmitButtonName).show();
selectStep(i + 1);
} else {
return false; // prevent moving on if the form is invalid
}
});
}