JQuery Validation: Validating Elements in Multiple

I have a form using the jQuery validation plugin.

<label>
    <input name="Distribution[]" type="checkbox" id="dist_europe" class="required minlength:1" value="Europe" />  Europe 
</label>

<select name="Europe[]" size="5" multiple id="Europe">
   <option value='Albania'>Albania</option>
   <option value='Andorra'>Andorra</option>
   <option value='Austria'>Austria</option>
</select>

I need to do a "conditional" check. eg. If checked, make sure that at least one item in the selection option is selected.

 $(document).ready(function(){
     $("#commentForm").validate({
      rules: {
              Europe: {
                required: "#dist_europe:checked",
                minlength: 1
              }  
      },
      messages: {
        Europe: "Please select at least 1 country"
      }
     }
})

Now I ran into a problem:

  • I can detect that the checkbox is checked.
  • However, I cannot check the array of 'select', Europe []
  • If I delete the array and name it Europe, I can find that at least one element is selected. But doing this will mean that the backend PHP script will not be able to handle multiple selections in the array.

How do I get around this? Thanks

+5
source share
1 answer

name="Europe[]" ( - , id ) rules messages, :

$(document).ready(function(){
     $("#commentForm").validate({
      rules: {
        'Europe[]': {
                required: "#dist_europe:checked",
                minlength: 1
              }  
      },
      messages: {
        'Europe[]': "Please select at least 1 country"
      },
     debug: true
    });
});

.

+12

All Articles