TheRealWitblitz
TheRealWitblitz

Reputation: 87

jQuery Validation plugin - Custom method comparing two select dropdowns

I have these two select type of HTML:

<label for="h_slat_type">Slat Type</label>
<select name="h_slat_type" id="h_slat_type">
                    <option disabled="disabled" selected>Select</option>
                    <option disabled="disabled">---------</option>
                    <option value="1">Type 1</option>
                    <option value="2">Type 2</option>
            </select>

<label for="v_slat_type">Slat Type</label>
<select name="v_slat_type" id="v_slat_type">
                    <option disabled="disabled" selected>Select</option>
                    <option disabled="disabled">---------</option>
                    <option value="1">Type 1</option>
                    <option value="2">Type 2</option>
            </select>

The condition for validation to fail is when I have both h_slat_type and v_slat_type set as 2.

In other words if:

h_slat_type 1 = v_slat_type 1 -> true

h_slat_type 2 = v_slat_type 1 -> true

h_slat_type 1 = v_slat_type 2 -> true

h_slat_type 2 = v_slat_type 2 -> false

The JS method:

jQuery.validator.addMethod("typecheck", function(value, element) {
    return ($('#h_slat_type').val() == $('#v_slat_type').val());
}, "Horizontal Type 2 and Vertical Type 2 incompatible!");

What would work in this case? ty vm.

Upvotes: 1

Views: 318

Answers (2)

Linga
Linga

Reputation: 10563

Try this:

    if($('#h_slat_type').val() == $('#v_slat_type').val())
       return true;
    else if ($('#h_slat_type').val()==1 && $('#v_slat_type').val()==2)
       return true;
    else if ($('#h_slat_type').val()==2 && $('#v_slat_type').val()==1)
       return true;
    else
       return false;

Upvotes: 1

Mouhamad Ounayssi
Mouhamad Ounayssi

Reputation: 361

You can try the code below:

{
var selection_1 = $('#h_slat_type').val();
var selection_2 = $('#v_slat_type').val();
if(selection_1=='2' && selection_2=='2') return false;
return true;
}

Upvotes: 0

Related Questions