8

I want to test if a CKEditor ( Rich Text ) field is empty as part of some business logic. I do not want to use the built in validation features.

If a CK Editor field has previously had text and then this text is deleted there is still content e.g.

<p dir="ltr">
 &nbsp;</p>

I can get a handle to this text string using :

dataVar = xspdoc.getDocument().getMIMEEntity(dataNamevar).getContentAsText();

Is there a way to test if the CKEditor field is empty of visible text ?

Sean Cull
  • 503
  • 3
  • 17

5 Answers5

2

Technically speaking, if it has what amounts to a a single visible newline in it as you've shown in your question, it isn't really "empty".

Realistically, you'll have to parse the content value to find out if there is content that is not either inside tags or the few special characters like   and so on.

I tend to do this in js, if I have to, by taking the whole string of text and splitting it into an array based on "<" then taking each element of the array and removing an text to the left of an ">", then trim. That leaves me an array of either empty strings or text that is outside any tags. From there it's easy enough check for any of strings in the array to see if they are not empty, and not " ".

This may be more cumbersome then some built in parser that I don't know, but it's fairly reliable and quick. (and a very similar method can be used in formula language as well).

In ssjs formula you could:

var checkString = @trim(@replacesubstring(@implode( @trim (@right( @explode( sourceHTMLstring , "<" ) , ">" ) ) , " "), "&nbsp;" , ""));

if(checkstring == "") {
        //  *** You have no content 
} else {
        // ***  you have content
}

Obviously this could be done just as easily in pure javascript, but the old formula language is so ingrained in my head, I'd go this way just out of habit.

** Also note: You may want to check for an <img> tag in there somewhere in case someone has done absolutely nothing other than put an image in the rich text.

Andrew Pollack
  • 140
  • 1
  • 11
  • Hi Andrew, I was hoping that there would have been something more "built in" as you have said but it appears not. Thanks for your help. – Sean Cull Apr 24 '12 at 05:54
0

Check if CKEditor is empty

For any browser var editor=CKEDITOR.instances.editorName.getData();

0

I found best answer for this

function validateCKEDITORforBlank(ckData)
{
   ckData = ckData.replace(/<[^>]*>|\s/g, '');
   var vArray = new Array();
   vArray = ckData.split("&nbsp;");
   var vFlag = 0;
   for(var i=0;i<vArray.length;i++)
   {
       if(vArray[i] == '' || vArray[i] == "")
       {
          continue;
       }
       else
       {
           vFlag = 1;
           break;
       }
    }
    if(vFlag == 0)
    {
       return true;
    }
    else
    {
       return false;
    }
}

Link

Azad
  • 5,144
  • 4
  • 28
  • 56
0

CKEditor has its own API, I guess this is the right method to use: http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#getData

Martin Jinoch
  • 308
  • 2
  • 7
  • 1
    It looks as though it will also have markup "The data will be in raw format". I am also ideally looking for something SSJS – Sean Cull Mar 10 '12 at 21:00
0

This might be helpful: http://xpagetips.blogspot.com/2011/10/be-careful-with-empty-ckeditor-rich.html

Russell Maher
  • 378
  • 1
  • 7
  • Hi Russell, I had seen that in my search for a solution prior to posting the question. It is exactly what we have seen but it doesn't address the issue of how to detect that the editor is empty after text has been deleted. Sean – Sean Cull Mar 11 '12 at 16:50