如何在 toeknize2 控制器上应用所需的验证?

How to apply required validation on toeknize2 controller?

我如何在 tokenize2 控制器上应用必填字段验证,这里的主要问题是它是 select 控件,但它显示为文本框。

插件 Link : https://zellerda.github.io/Tokenize2/index.html

代码:

<select class="tokenize-demo" multiple>
  <option value="1">Africa</option>
  <option value="2">Americas</option>
  <option value="3">Asia</option>
  <option value="4">Europe</option>
  <option value="5">Oceania</option>
</select>
<script>
  $('.tokenize-demo').tokenize2();
</script>

为 select 字段添加 ID 和名称

<select class="tokenize-demo" name="tokenize_demo[]" id="tokenize_demo" multiple>
<label class="error" for="tokenize_demo" id="tokenize_demo-error"></label>

我使用 jquery 验证器来验证

$.validator.addMethod("required", function (value, element){      
  if ($("#tokenize_demo").val() != null && $("#tokenize_demo").val() != "")
  { 
    $(".has-error").removeClass("has-error") 
    $("#tokenize_demo-error").css("display", "none");    
    return true;
  }
  else
  {  
    return false;
  };
});

$('#form_id').validate({
ignore: [],
onkeyup: false,
highlight: function(element) {
  $(element).closest('div').removeClass('has-success').addClass('has-error');
},
success: function(element) {
  $(element).closest('div').removeClass('has-error');
},
rules: {     
  "tokenize_demo[]": {
    required: true
  }
},
messages: {
  "tokenize_demo[]": {
    required: "Field is required."
  }
},
submitHandler: function(form) {      
  form.submit();
}
});