How to check whether CKEditor has some text in it?

后端 未结 6 1548
时光取名叫无心
时光取名叫无心 2021-02-04 02:08

I have an HTML form with a few fields. One of them is a textarea managed by CKEditor.

When the user wants to submit the form, I want to check whether he entered values

6条回答
  •  南笙
    南笙 (楼主)
    2021-02-04 02:15

    We use prototype, with that library and the following addition:

    Element.addMethods({  
      getInnerText: function(element) {
        element = $(element);
        return element.innerText && !window.opera ? element.innerText
          : element.innerHTML.stripScripts().unescapeHTML().replace(/[\n\r\s]+/g, ' ');
      }
    });
    

    (Thanks to Tobie Langel)

    I was able to use the following function to determine whether any actual text was inside CKEditor:

    function editorEmpty(instanceName){
    
        var ele = (new Element('div')).update(CKEDITOR.instances[instanceName].getData()); 
    
        return (ele.getInnerText() == '' || innerText.search(/^( )+$/i) == 0);
    }
    

    Note the test for   as well - often when the editor appears empty, it actually contains something like:

     

    .

提交回复
热议问题