jQuery, Validate A or B but not A and B
I am using the jquery validate plugin and am attempting to validate a select or an inp开发者_StackOverflow中文版ut but not both. This is what I started with and here is my pseudo working example.
//makes only one field required state or territory
$('.state_territory2').bind('change', function() {
if ( $(this).is('select:selected') ) { // state selected
$('input.state_territory2').removeClass("required");
}
if ( $(this).is('input:filled') ) { // province filled
$('select.state_territory2').removeClass("required");
}
});
and here is the html
<input type="text" id="province" name="province" class="inputs state_territory2">
and
<select id="state" name="state" class="state_territory2">
I got the code working but now it requires the input to always be required even if the state is selected. This is a bit above my head so any help would be appreciated.
Note that i am not familiar with the plugin you're using, but i'm missing an else
and two addClass
'es off the top of my head:
//makes only one field required state or territory
$('.state_territory2').bind('change', function() {
if ( $(this).is('select:selected') ) { // state selected
$('input.state_territory2').removeClass("required");
$('select.state_territory2').addClass("required");
} else if ( $(this).is('input:filled') ) { // province filled
$('select.state_territory2').removeClass("required");
$('input.state_territory2').addClass("required");
}
});
精彩评论