Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

javascript - CK Editor validates second time

I have two CKEditors in my HTML (I mean to say multiple ckeditors).

Also I am using Validate plugin for checking if CKEditor is empty,if empty show error.

Validation works perfectly, but it validates second time, whereas it should validate first time itself.

I have checked all the questions and answers here, but none helped.

I created a JS Fiddle.

Code for validate :

HTML

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

    <p>
        <label for="editor1">
            Editor 1:
        </label>
        <textarea class="ckeditor" cols="80" id="editor1" name="editor1" rows="10"></textarea>
    </p>
    <p>

    </p>

    <p>
        <label for="editor1">
            Editor 2:
        </label>
        <textarea class="ckeditor" cols="80" id="editor2" name="editor2" rows="10"></textarea>
    </p>
    <p>
        <input type="submit" value="Submit">
    </p>

</form>

Script

$(document).ready(function(){   
// validate signup form on keyup and submit
    $("#frmEditor").validate({
        ignore: [],
        debug: false,
        rules: {
            editor1:{
                required: true
            },
            editor2:{
                required: true
            }           
        },
        messages: {         
            editor1: {
                required: "Please enter"
            },
            editor2: {
                required: "Please enter"
            }           
        },
        submitHandler: function(form) {
            form.submit();
        }       
    });
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to update instance before validation performs. So first add an id attribute or class on button like this

<input type="submit" value="Submit" id="submitFormBtn">

Now in your js code write below function

$('#submitFormBtn').click(function () {
    CKEDITOR.instances.editor1.updateElement();
    CKEDITOR.instances.editor2.updateElement();
});

Hope this will work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...