Search code examples
phphtmlwysiwygrich-text-editor

How to validate rich text editor source


I am using CLEditor in my application for rich text that the user will fill in.

When I validate the textarea element in my PHP server side script, I do all the checking e.g. if the element is empty.

There is a problem with this in that the source of the text editor sometimes remains despite the text being cleared.

Here is an example of what I mean:

Say I have a rich text editor as shown below and I have the heading 'write your first page!'.

enter image description here

Here is how the source looks for the above picture: enter image description here

Then when I remove the text only and NOT any empty lines remaining, the source around the text remains as shown below:

enter image description here

The PHP empty function is going not going to pass as it's technically not empty because of the source code contained in the text editor.

The issue with this is I cannot rely on the user to clear the whole screen including empty lines, so is there a way I can get rid of any source that doesn't contain actual content as the second image above shows?

HTML:

<textarea id="textarea" name="chapter"><h4 style="text-align: center;">Write your first page!</h4></textarea>

Solution

  • If I understood well your problem, you want to know if there is some text or just HTML tags.

    In PHP, you can do the follow:

    $text = trim(strip_tags($chapter));
    if ($text == ''){
       // just HTML tags
    }
    

    The strip_tags function removes the HTML tags.

    The trim function remove white spaces, break lines, etc...