I'm working on a dynamic form where based on a visitor's selection of radio buttons certain <div> text and additional fields are shown or hidden. I would like to make some of these additional fields required but only if they are visible. Problem is: I can read php more than I can write it and jquery is a tad beyond my learning curve.
With the help of some Daniweb users I came up with the script below for the form here. When the form is submitted it returns a validation error and I'm not sure how to tweak the php code to check if parameter > 0
and if so then it should be validated. I'm using the Contact Form 7 wordpress plugin and Jquery Validation For Contact Form 7 but neither seems to deal directly with this type of validation.
$.fn.toggleFields = function(condition) {
return this.each(function(i, el) {
el = $(el);
condition = (condition != null) ? condition : !el.is(':visible');
el[condition ? 'show' : 'hide']().find(':input').attr('disabled', !condition);
});
};
$(document).ready(function(){
var dadAddr = $('#dad_address'),
momAddr = $('#mom_address'),
dadTrigger = $('#dadaddress'),
momTrigger = $('#momaddress'),
togetherTrigger = $('#bothparents');
// hide and disable fields
dadAddr.toggleFields(false);
momAddr.toggleFields(false);
togetherTrigger.click(function() {
dadAddr.toggleFields(false);
momAddr.toggleFields(false);
});
dadTrigger.click(function() {
dadAddr.toggleFields(this.checked);
momAddr.toggleFields(false);
});
momTrigger.click(function() {
momAddr.toggleFields(this.checked);
dadAddr.toggleFields(false);
});
$('#591').validate();
});
The php code for the Jquery Validation For Contact Form 7 plugin is below but I'm not sure which php code to post for the Contact Form 7 plugin since most of the files says (inactive) next to their name when I open them in the Wordpress plugin editor.
//add_action('admin_enqueue_scripts', 'jvcf7_validation_js');
$jvcf7_show_label_error = get_option('jvcf7_show_label_error');
$jvcf7_highlight_error_field = get_option('jvcf7_highlight_error_field');
$jvcf7_hide_contact_form_7_validation_error = get_option('jvcf7_hide_contact_form_7_validation_error');
if (empty($jvcf7_show_label_error)){
update_option('jvcf7_show_label_error', 'yes');
$jvcf7_show_label_error = get_option('jvcf7_show_label_error');
}
if (empty($jvcf7_highlight_error_field)){
update_option('jvcf7_highlight_error_field', 'yes');
$jvcf7_highlight_error_field = get_option('jvcf7_highlight_error_field');
}
if (empty($jvcf7_hide_contact_form_7_validation_error)){
update_option('jvcf7_hide_contact_form_7_validation_error', 'yes');
$jvcf7_hide_contact_form_7_validation_error = get_option('jvcf7_hide_contact_form_7_validation_error');
}
$styleSheet = '.wpcf7-form span.wpcf7-not-valid-tip{ display:none !important;}';
if ($jvcf7_show_label_error == 'yes'){
$styleSheet.='.wpcf7-form label.error{color:#900; font-size:11px; float:none;}';
} else {
$styleSheet.='.wpcf7-form label.error{display:none !important;}';
}
if ($jvcf7_highlight_error_field == 'yes'){
$styleSheet.='.wpcf7-form input.error, .wpcf7-form select.error, .wpcf7-form textarea.error{border-bottom:2px solid #900;outline: none;}';
}
add_action('wp_enqueue_scripts', 'jvcf7_validation_js');
function jvcf7_validation_js(){
global $styleSheet;
echo '<script> jvcf7_loading_url= "'.plugins_url('contact-form-7/images/ajax-loader.gif').'"</script>';
wp_dequeue_script( 'contact-form-7' );
wp_enqueue_script('jquery-form');
wp_enqueue_script('jvcf7_jquery_validate', plugins_url('jquery-validation-for-contact-form-7/js/jquery.validate.min.js'), array('jquery'), '', true);
wp_enqueue_script('jvcf7_validation_custom', plugins_url('jquery-validation-for-contact-form-7/js/jquery.jvcf7_validation.js'), '', '', true);
echo '<style>'.$styleSheet.'</style>';
}
include('plugin_interface.php');
?>
Thanks in advance. Sorry for the newbie question.