Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery validate not working with chosen plugin for multiple select

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/

like image 414
coure2011 Avatar asked Sep 16 '13 10:09

coure2011


1 Answers

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');
    });
});
like image 55
Louis Huppenbauer Avatar answered Oct 22 '22 03:10

Louis Huppenbauer