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

时间:2017-07-18 09:46:02

标签: jquery tokenize2

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

插件链接: 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>

1 个答案:

答案 0 :(得分:1)

为选择字段添加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();
}
});