I know this might be considered off topic but I am not sure where else to post this as it also pertains to WordPress as well as the plugin Contact Form 7. If you know where I should post this other than here please tell me.
I am looking to have a conditionally required field. I have found code to work with but I think I am confusing myself and need some outside perspective.
What I need to work is if my check box is checked to "yes" it will make the drop down next to it required and if the check box is checked to "no" it is not required.
This is what I have as my form with the shortcode on the backend:
If you have ordered from us in the past, do you already work with one of our outside sales representatives?
[checkbox* check-sales id:checksales "Yes" "No"]
If you checked "Yes" which representative do you generally deal with?
[select sales-rep id:sales include_blank "Marla" "Lisa" "Wendy" "Stacy" "Nicole" "Linda" "Jody" "Gisele" "Ray" "Craig"]
[submit]
And here is the php code example that goes into the functions.php file.
function is_gmail($email) {
if(substr($email, -10) == '@gmail.com') {
return true;
} else {
return false;
};
};
function custom_email_validation_filter($result, $tag) {
$type = $tag['type'];
$name = $tag['name'];
if($name == 'your-email') { // Only apply to fields with the form field name of "your-email"
$the_value = $_POST[$name];
if(!is_gmail($the_value)){
$result['valid'] = false;
$result['reason'][$name] = 'This is not a gmail address!'; // Error message
};
};
return $result;
};
add_filter('wpcf7_validate_email','custom_email_validation_filter', 10, 2); // Email field
add_filter('wpcf7_validate_email*', 'custom_email_validation_filter', 10, 2); // Required Email field
I know this is geared towards email checking. However what I am confusing myself on is what I need to change in order to make this work for a checkbox and a dropdown.