I have a form that, where depending on the selected value of the userAction radio group, either the loginFields or signupFields are shown. Also, I have two error containers - errorMessageLogin and errorMessageSignup - to display error messages respective to the selected value of the userAction:
<div id="errorsSection">
<div id="errorMessageLogin">
<!-- Login Error Messages go here -->
</div>
<div id="errorMessageSignup">
<!-- Signup Error Messages go here -->
</div>
</div>
<div id="contentSection">Lorem ipsum dolor sit amet...</div>
<div id="formSection">
<form id="myForm">
<input name="userAction" type="radio" value="login" checked="checked">Login</input>
<br />
<input name="userAction" type="radio" value="signup">Sign Up</input>
<div id="loginFields" style="display:none">
<!-- Login fields: email address and, password -->
</div>
<div id="signupFields" style="display:none">
<!-- Signup fields: name, age, email address, and password -->
</div>
</form>
</div>
I'm using the jQuery Validation Plugin like this:
$("#myForm").validate({
submitHandler: function (form) {
MyWebService.DoWork(form);
},
rules: myRules,
errorLabelContainer: null, // how to determine at runtime?
messages: myMessages,
highlight: function (element, errorClass) {
$(element).prev().addClass(errorIcon);
$(element).addClass(errorClass);
$(element.form).find("label[for=" + element.id + "]").addClass(errorClass);
},
unhighlight: function (element, errorClass) {
$(element).prev().removeClass(errorIcon);
$(element).removeClass(errorClass);
$(element.form).find("label[for=" + element.id + "]").removeClass(errorClass);
}
});
I highlight invalid fields by changing the elements class and add a small icon nearby; this works great. However, in addition to the highlighting, I'd like to display the messages defined in myMessages in either errorMessageLogin or errorMessageSignup based on the selection in the userAction radio group. However, it appears that the value of errorLabelContainer is to be a jQuery object and not a function, which is unfortunate, since I'd be able to determine which error to show in a function.
Is it possible somehow to toggle the error container to be used by the jQuery Validation plugin?