1

I'm using multiselect js plugin in select element and jquery validate plugin to validate form elements.

I know this is because of '[]' in select element name attribute, that is causing this error, but I want to enable multiple selections.

When I remove '[]', it is working fine..

Here's my HTML:

<div class="form-group">
    <label class="col-sm-4 control-label" for="internal_team">Team <span class="input-required">*</span></label>
         <div class="col-sm-7">
              <select name="team[]" class="form-control multiselect-ui" multiple>
              <option value="1">1</option>
              <option value="2">2</option>
              <option value="3">3</option>
              </select>
         </div>
</div>

Here's my jquery validate:

rules: 
{
   "team[]": "required"
},
messages:
{
     "team[]": "Please select team",
}

How do I solve this issue?? Any help is very much appreciated. Thanks.

Sparky
  • 98,165
  • 25
  • 199
  • 285
Azima
  • 3,835
  • 15
  • 49
  • 95

2 Answers2

2

Have you tried assigning an id to select tag and retrieving values with multiselect method?

<select name="team[]" class="form-control multiselect-ui" id="team" multiple="multiple">

$('#team').multiSelect()
Rosy Shrestha
  • 1,359
  • 10
  • 10
1

You have to escape the [] because jQuery thinks its an attribute selector.

$("label[for='team[]'], label[for='team[]'] *, #team[]-error")

Should be

$("label[for='team[]'], label[for='team[]'] *, #team\\[\\]-error")

demo

$("label[for='team[]'], label[for='team[]'] *, #team\\[\\]-error")
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Carsten Løvbo Andersen
  • 26,637
  • 10
  • 47
  • 77