Validation: Only validate textbox if corresponding checkbox checked
I have a simple form which I am running a validation on a single text box (as shown below, adapted from the milk example).
- $(document).ready(function() {
- // validate signup form on keyup and submit
- var validator = $("#updateModules").validate({
- rules: {
- guestbookContact: {
- required: true,
- email: true
- },
- },
-
- // the errorPlacement has to take the table layout into account
- errorPlacement: function(error, element) {
- if ( element.is(":radio") )
- error.appendTo( element.parent().next().next() );
- else if ( element.is(":checkbox") )
- error.appendTo ( element.next() );
- else
- error.appendTo( element.parent().next() );
- },
- // specifying a submitHandler prevents the default submit, good for the demo
- submitHandler: function() {
- form.submit();
- },
- // set this class to error-labels to indicate valid fields
- success: function(label) {
- // set as text for IE
- label.html(" ").addClass("checked");
- }
- });
-
Now I only want to validate the text box if the checkbox "requireEmail" is checked. Is there an easy way to do this?