gomesh munda
gomesh munda

Reputation: 850

tinyMCE empty validation check

In one of my PHP webpage, i have a tinyMCE editor.Now, before hitting the 'Save' button, i am checking whether the tinyMCE editor is empty or not using the javascript as shown below:

HTML:

<input type="submit" id="auBtn" name="auBtn" style="display: block;" value='<?php echo $subBtnCap; ?>' onclick="return validate();"/>

Javascript:

function validate(){
    if ((tinymce.EditorManager.get('bDesc').getContent()) != ''){
       alert('Blog Description can not be empty.');
       return false;
    }
}

The above javascript code alerts me when the editor is empty.However, it still shows the alert message when the editor is not empty.Can you please tell me what could be wrong with my code.Thanks in advance.

Upvotes: 0

Views: 4668

Answers (2)

itsyub
itsyub

Reputation: 113

Tinymce returns empty <p></p> tag when there is nothing in tinymce. you can do one thing, just add config option forced_root_block:"" at initialization of tinymce. For example,

tinyMCE.init({
            height: 250,
            selector: 'textarea',
            forced_root_block: "",
            });

Above config option remove default root tag and now tinymce returns empty string.

And also change condition in your code

function validate() {
        if (tinymce.EditorManager.get('bDesc').getContent() === '') {
            alert('Blog Description can not be empty.');
            return false;
        }
    }

Upvotes: 1

von Oak
von Oak

Reputation: 833

You have wrong condition. Instead != try ==

(tinymce.EditorManager.get('bDesc').getContent()) == '')

Whole working example:

<script src="http://cdn.tinymce.com/4/tinymce.min.js"></script>
<script>tinymce.init({
        selector: 'textarea',
        plugins: 'wordcount',
    });

    function validate() {
        if ((tinymce.EditorManager.get('bDesc').getContent()) == '') {
            alert('Blog Description can not be empty.');
            return false;
        }
    }
</script>   

<form method="POST" action="">
    <textarea  name="bDesc"></textarea>
    <input type="submit" id="auBtn" name="auBtn" style="display: block;" value='validate' onclick="return validate();"/>
</form> 

Upvotes: 2

Related Questions