最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JQuery validation : How to check items in multiple select - Stack Overflow

programmeradmin5浏览0评论

I have a form using 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 'conditional' validation. eg. If checkbox is selected, make sure at least one item in 'select option is selected'.

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

The issue I am facing now is :

  1. I am able to detect that the checkbox is selected.
  2. However, I am not able to check the 'select' array, Europe[]
  3. If I remove the array, and name it Europe, I will be able to detect that at least one item is selected. But, doing that will mean the backend PHP script will not be able to process the multiple select in the array.

How do I work around this? Thanks

I have a form using 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 'conditional' validation. eg. If checkbox is selected, make sure at least one item in 'select option is selected'.

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

The issue I am facing now is :

  1. I am able to detect that the checkbox is selected.
  2. However, I am not able to check the 'select' array, Europe[]
  3. If I remove the array, and name it Europe, I will be able to detect that at least one item is selected. But, doing that will mean the backend PHP script will not be able to process the multiple select in the array.

How do I work around this? Thanks

Share Improve this question edited Sep 29, 2012 at 10:48 Eli 14.8k5 gold badges61 silver badges77 bronze badges asked Oct 12, 2010 at 16:02 slokslok 631 gold badge1 silver badge3 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 14

When using a name like name="Europe[]", you'll need to use a string for your identifier (the identifier is the name, not the id of the element) in rules and messages, like this:

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

You can test it out here.

发布评论

评论列表(0)

  1. 暂无评论