jQuery验证:如何使字符数量不包括CKEditor生成的标记?

时间:2015-05-18 01:09:53

标签: javascript jquery ruby-on-rails jquery-validate ckeditor

在我的Rails应用程序中,我想为CKEditor的数据使用jQuery Validation插件,除a little inappropriate count外,它的工作正常,包括CKEditor生成的令牌。以下是主要代码:

http://jsfiddle.net/sscs1kbx/3/

HTML:

  <textarea name="answer[content]" class="required" id="editor1"></textarea>
  <script> CKEDITOR.replace('answer[content]');</script>
  <br />

的javascript:

$('#add-job').validate({
   ignore: [],         
   rules: {
          "answer[content]": {required: function(){CKEditorUpdate();},
                            minlength: 18 }
        },
   messages: {
                "answer[content]": "Required at least 10 characters"
             },
  });

function CKEditorUpdate(){
    for(instance in CKEDITOR.instances){
       CKEDITOR.instances[instance].updateElement();
     }
}

此处minlength设置为minlength: 18,但我只能输入11个字符ddddddddddd,所以我猜最终textarea的字符数包括{{{}等标记1}},但我希望jQuery Validation只包含用户输入的字符,我该怎么办?

1 个答案:

答案 0 :(得分:1)

我终于解决了,因为只是设置一些参数无法排除html令牌,我使用addMethods为其指定了一个名为minlengthxo的方法:http://fullcalendar.io

jQuery.validator.addMethod("minlengthxo", function (value, element,param) {
  originalVal = CKEDITOR.instances.editor1.getData().replace(/<[^>]*>/g, '');
  var length = $.isArray( originalVal ) ? value.length : this.getLength( originalVal, element );
  return length >= param;
}, "default errorMessage");

$('#add-job').validate({

ignore: [],         
rules: {
        "answer[content]": {
            required: function() 
                     {
                       CKEditorUpdate();                        
                     }, 
                     minlengthxo: 18
                }
            },
            messages: {
                "answer[content]": "You need input at least 18 characters, now you entered "
            },
        });

    function CKEditorUpdate(){
       for(instance in CKEDITOR.instances){
          CKEDITOR.instances[instance].updateElement();
    }
}