views:

47

answers:

1

I am using the jquery validate plugin and am attempting to validate a select or an input 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.

+2  A: 

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");
        }
    });
Kris
You were right on the money it looked like all i needed was the else if thank you!
CarterMan
You're welcome!
Kris