I’m working with the jQuery validation plugin and need help with a specific issue. I have a variable that stores CSS classes that should be ignored during validation, but I can’t figure out how to pass this variable into the validate method.
Here’s my current setup:
$(document).ready(function(){
var skipElements = "";
// Show/hide sections based on user type selection
$("#userType").change(function() {
switch ($(this).val()){
case 'new-client':
$(".section_all").hide();
$("#section_1").show();
skipElements = '.field2,.field4,.field5';
break;
case 'existing-client':
$(".section_all").hide();
$("#section_2").show();
skipElements = '.field1,.field4,.field5';
break;
case 'partner-inquiry':
$(".section_all").hide();
$("#section_3").show();
break;
case 'job-seeker':
$(".section_all").hide();
$("#section_4").show();
skipElements = '.field1,.field2,.field5';
break;
default:
$(".section_all").hide();
}
});
// Form validation setup
$("#contactForm").validate({
// How do I use the skipElements variable here?
// ignore: skipElements
});
});
Also, the validation only triggers when users interact with the dropdown. If someone fills out their details and submits without touching the dropdown, no validation happens at all. How can I fix both of these problems?