1

I am trying to validate a form with a couple of elements being required if a checkbox is NOT checked.

Now if I submit the form the rules fire - and then I check the checkbox on - the validation rules have already fired - and even though the checkbox is now checked - the validation rules still apply and the form will not submit until I enter the fields.

What I was hoping was that the checkbox could toggle the rules on or off. Any help much appreciated.

var validator = $(".cmxform").validate({
                rules: {
                    txtAddress1: { 
                        required: function(){
                            $('#chkCurrentEmployer').attr('checked') !== "true"
                    } },
                    txtAddress2: {
                        required: function(){
                            $('#chkCurrentEmployer').attr('checked') !== "true"
                    } }
                }
            });

1 Answer 1

2

Use the required( dependency-expression ) rule

var validator = $(".cmxform").validate({
  rules: {
    txtAddress1: {
      required: "#chkCurrentEmployer:checked"
    },
    txtAddress2: {
      required: '#chkCurrentEmployer:checked'
    }
  }
});

Documentation here: http://docs.jquery.com/Plugins/Validation/Methods/required#dependency-expression

Your code might also work if those functions return something like this

return $('#chkCurrentEmployer').attr('checked');
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, also i found the "invalidHandler" method which runs when there are errors. Does any handler or events run when things ARE VALID? I want my submit button faded out until everything passed validation, ideaS?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.