0

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

1 Answer 1

4

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;
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.