Search code examples
javascriptjquerytinymce

How can I check if textarea (tinymce) only contains space?


I have a form using Tinymce, I need to check if it only contains space.

I try using this function

function validation_form()
{
    var content = tinyMCE.get('main-comment').getContent().replace(' ','');
    if(content == "" || content == null || content == '<p> </p>') {
        return false;
    }
}

But it returns true when I input several spaces and submit, I want it to return false instead.

Can anyone help me? Thanks,


Solution

  • use $.trim ,

    it is clean and readable.

    function validation_form()
    {
        var content = $.trim(tinyMCE.get('main-comment').getContent({format: 'text'}));
        if(content.length == 0) {
            return false;
        }
       return true;
    }
    

    Updated: format as text, to get text content from editor. check fiddle