jquery validate not working with chosen plugin for multiple select

coure2011 picture coure2011 · Sep 16, 2013 · Viewed 9.9k times · Source

I am trying to make a select box of type multiple as required using jquery validate but its not working. Here is my code

$(document).ready(function(){
    $('#test').chosen();
    $('#testForm').validate();
    $('#validateIt').click(function(){
        if($('#testForm').valid())
            alert('Valid');
        else
            alert('Invalid');
    });
});

html

<form id="testForm">
<select required style="width: 165px;" id="test" name="test" class="chzn-select multiselect" multiple="multiple">
    <optgroup label="Lab1"><option value="2">Some Program</option></optgroup>
</select>
<input type="button" id="validateIt" value="Run"/>
</form>

jsfiddle: http://jsfiddle.net/jm8Dd/

Answer

Louis Huppenbauer picture Louis Huppenbauer · Sep 16, 2013

The problem here is actually quite simple. The jQuery-Validator plugin ignores hidden input fields per default.

Telling it not to ignore hidden fields should make it work.

$(document).ready(function(){
    $('#test').chosen();
    $('#testForm').validate({ ignore: ":hidden:not(select)" });
    $('#validateIt').click(function(){   
        if($('#testForm').valid())
            alert('Valid');
        else
            alert('Invalid');
    });
});