select2 validation - Select a minimum of one value

非 Y 不嫁゛ 提交于 2020-01-04 17:17:41

问题


I am using select2 and jQuery Validation plugins.

HTML for select2:

<div class="form-group">
<label for="select2Region"><h3>Set Location</h3></label><br />
<input type="hidden" id="select2Region" style="width:40%" required/>
</div>

select2 Initiation:

        $('#select2Region').select2({
            minimumInputLength: 1,
            data: $.parseJSON(selectRegion),
            placeholder: "Enter your City here",
            allowClear: true

        });

Data is populating perfectly fine. Now, i want users to select a value.

As is mentioned in Github, i tried this:

Try adding ignore: '', in the line directly above rules:. An empty string did the job for me since ignore: null, wasn't working.

and this as well:

$('.select2').select2().change(function(){
   $(this).valid();
});

But they both are not working for me. What is the proper way of doing this?


回答1:


jQuery Validate Code

        $("#myForm").validate({
          ignore: '',
          messages: {
            select2Start: {
              required: "Please select your starting point.",
            }
          }
        });

n my HTML code goes like this:

<div class="form-group">
  <label for="select2Region"><h3>Set Location</h3></label><br />
  <input type="hidden" id="select2Region" name="select2Region" style="width:40%" required/>
</div>

Hope that helps!



来源:https://stackoverflow.com/questions/21344481/select2-validation-select-a-minimum-of-one-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!