I am attempting to use the JQuery Validation Plugin but am running in to a small problem. My form uses the following method to hide/show the desired content:
<div id="page1" class="page" style="visibility:visible;">
...Page 1 form content...
</div>
<div id="page2" class="page">
...Page 2 form content...
</div>
Form has two pages...here is the button on Page 1 that allows the user to Continue.
<input type="button" id="C1" value="Continue" onClick="showLayer('page2')">
The showLayer() function simply toggles the visibility of the passed element. The only content that needs to be validated is on page 1, where this button is located. What I would like to do is have the Continue button first call .validate() and then upon successful validation proceed to the second page. Research has shown me that it is an easy thing to have the onClick call multiple functions like so:
<input type="button" id="C1" value="Continue" onClick=".validate()";"showLayer('page2')">
While this "technically" works, the user finds himself viewing page 2 content with no clue that he/she is being presented validation errors back on page 1 unless they click the "Go Back" button. Obviously not a very usable situation!
I have tried this but am still faced with the same basic problem.
<input type="button" id="C1" value="Continue" onClick="formValidate()">
function formValidate() {
.validate();
showLayer('page2');
}
What is needed is to determine whether the validation function came back successfully and then conditionally continue on to page 2 assuming all validations pass. After research into the plugin documentation I have come across the following to be my most likely (I think) candidate to achieve this:
"invalidHandler"
$(".selector").validate({
invalidHandler: function(event, validator) {
var errors = validator.numberOfInvalids();
if (errors) {
var message = errors == 1
? 'You missed 1 field. It has been highlighted'
: 'You missed ' + errors + ' fields. They have been highlighted';
$("div.error span").html(message);
$("div.error").show();
} else {
$("div.error").hide();
}
}
});
...but I don't know how to tie it together with the conditional checking needed. I don't need to display the error count as the example shows but it seems to do the proper callback with an error count that (I would think) provide the basis for the success check and the framework for the conditional. I may very well be barking up the wrong tree and there may be a much better way to accomplish this... Any ideas? All help much appreciated!
UPDATE *************** JSFiddle http://jsfiddle.net/uEh3a/ Not sure if I have done this right...first time!
Before I added the code from Sparky the form did some automatic calcs based on the radio buttons selected. After code added no calcs and cannot move between forms. I'm sure I've done something dumb...can anybody see what?