Using jquery validation plugin to validate multiselect box created using bootstrap multiselect plugin?
I am using jquery validation plugin and lately I had to add multi-select dropdown in my form. I used davidstutz bootstrap multiselect plugin. However, could not validate the multiselect box below is my html code , I submit with selecting a value from multiselect box without giving other form inputs even then form gets submitted,
<select name="franchisee_course" class="multiselect" multiple="multiple" id="franchisee_course" size="1" placeholder="Courses">
<?php
foreach($courses as $course) {
echo "<option value=".$course['course_id']." >".$course['course_description']."</option>";
}
?>
</select>
and my calling multi-select,
$('.multiselect').multiselect({
noneSelectedText: 'Select Courses',
includeSelectAllOption: true,
enableFiltering: true,
filterPlaceholder: 'Search course'
});
and these are my validation codes for multiselect,
$.validator.addMethod("needsSelection", function(value, element) {
return $(element).multiselect("getChecked").length > 0;
});
$.validator.messages.needsSelection = 'Select Atleast One Course';
franchisee_course: {
needsSelection: true,
required:true
},
ignore: ':hidden:not("#franchisee_course")', // I don't know what this line does
jQuery
twitter-bootstrap
validation
- asked 9 years ago
- Gul Hafiz
1Answer
The problem is that the "getChecked" argument doesn't do anything anymore. Here is an updated, working version of the validation method
$.validator.addMethod("needsSelection", function (value, element) {
var count = $(element).find('option:selected').length;
return count > 0;
});
- answered 9 years ago
- Gul Hafiz
Your Answer