• I’m attempting to make fields required based on a checkbox selection.

    In the Contact Form 7 input box I’ve got neither email nor phone selected as required.

    When the ’email’ checkbox is clicked, the email field should become required, then the ‘phone’ checkbox is clicked it becomes required and the email is not.

    So far the below code resets the title to show the asterisk, and adds the class ‘wpcf7-validates-as-required’ which is what I believe makes the field subject to validation.

    (email is the first checkbox, I’m only showing the relevant code)

    $(".contact-checkbox input:checkbox:first").change(function() {
        if(this.checked) {
            $('span.email').find('input').prop('title', 'Email*');
            $('span.email').find('input').addClass('wpcf7-validates-as-required');
        }
    });

    Problem is this doesn’t work, the field doesn’t validate as required. What am I missing?

  • The topic ‘Contact Form 7 Conditional Validation’ is closed to new replies.