How can I check if the CK editor field is empty?

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

If there was previously text in the CK Editor field and then this text was deleted, there is still content, for example.

<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 check if the CKEditor field has visible text?

+6
source share
5 answers

From a technical point of view, if it has something that makes up one visible new line in it, as you showed in your question, this is not entirely "empty".

Actually, you need to analyze the meaning of the content in order to find out if there is content that is not inside the tags, nor a few special characters, such as etc.

I try to do this in js, if necessary, taking the entire line of text and breaking it into an array based on "<" then taking each element of the array and deleting the text to the left of ">", then trim. This leaves me with an array of either blank lines or text that is outside of any tags. From there itโ€™s easy enough to check if there are any rows in the array, if they are not empty, and not "".

It may be more cumbersome than some parsers that I donโ€™t know, but it is quite reliable and fast. (and a very similar method can be used in the language of the formula).

In the ssjs formula you can:

 var checkString = @trim(@replacesubstring(@implode( @trim (@right( @explode( sourceHTMLstring , "<" ) , ">" ) ) , " "), "&nbsp;" , "")); if(checkstring == "") { // *** You have no content } else { // *** you have content } 

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

** Also note: you can check the <img> tag there somewhere if someone has done absolutely nothing but put the image in rich text.

+2
source

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

0
source

Make sure CKEditor is empty.

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

0
source

I found a better 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

0
source

Source: https://habr.com/ru/post/910408/


All Articles