CkEditor中的Jquery验证

时间:2017-02-28 09:16:31

标签: php ckeditor jquery-validate

我已经看过这个例子,并在我的代码中应用它,但没有任何效果,它不起作用。

Referral ans-1

Referral ans-2

我需要在ckeditor中应用jquery验证,我已经看到了所有那些过去的例子,即使我已经提到了上面的那些链接,通过这一步我的验证仍然无效。

这是我的HTML代码

 <div class="elementbox">
                <label class="form-label">Content<span class="required">*</span></label>
                <div class="controls">
                    <textarea name="content_body" id="content_body" rows="10" cols="80"><?php echo $content_body; ?></textarea>
                </div>
            </div>

<script>
var url = "<?php echo base_url(); ?>";


CKEDITOR.replace( 'content_body',{
        //extraPlugins: 'imageuploader'
      } );
</script>

我的Jquery验证码

$("#add_content_pages").validate({
        ignore: [],
        debug: false,
        rules: {
             title: {
                required: true
            },
            content_id: {
                required: true
            },
            content_body:{
                required: function() 
                {
                    CKEDITOR.instances.content_body.updateElement();
                }
            }
        },
        messages: {
             title: {
                required: "Please enter Title"
            },
            content_id: {
                required: "Please Select Content Type"
            },
            content_body: {
                required: "Please enter Content"
            }
        },
        errorPlacement: function (error, element) {
            var attr_name = element.attr('name');
            if (attr_name == 'type') {
                error.appendTo('.type_err');
            } else {
                error.insertAfter(element);
            }
        }

    });

任何解决方案我缺少什么?

1 个答案:

答案 0 :(得分:1)

请检查此代码

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/jquery.validation/1.15.0/jquery.validate.min.js"></script>
<script src="https://cdn.ckeditor.com/4.6.2/standard/ckeditor.js"></script>

然后是html表单

<form action="" method="post" id="check_form">

<div class="elementbox">
    <label class="form-label">Content<span class="required">*</span></label>

    <div class="controls">
        <textarea name="content_body" id="content_body" rows="10" cols="80"></textarea>

        <div id="error_check_editor"></div>
    </div>
</div>
<script>
    CKEDITOR.replace('content_body');
</script>
<br/>
<input name="submit" type="submit" value="Submit" class="button" id="submit"/>

 </form>

然后脚本

<script>

$.validator.addMethod("check_ck_add_method",
    function (value, element) {
        return check_ck_editor();
    });

function check_ck_editor() {
    if (CKEDITOR.instances.content_body.getData() == '') {
        return false;
    }
    else {
        $("#error_check_editor").empty();
        return true;
    }
}


$(document).ready(function () {

    $("#check_form").validate(
        {
            ignore: [],
            debug: false,
            errorPlacement: function (error, element) {
                if (element.attr("name") == "content_body") {
                    error.insertAfter(element);
                }
            },
            rules: {

                content_body: {
                    check_ck_add_method: true
                }
            },
            messages: {

                content_body: {}
            }
        });
});


</script>