Javascript 如果 TinyMCE 编辑器是空白的,我如何通过在它旁边附加一个字符串来验证它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8749179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:19:43  来源:igfitidea点击:

How do I validate a tinyMCE editor, if it is blank by appending a string next to it?

javascriptjquerytinymcevalidation

提问by Mike

I need to validate a form. This form has some dropdowns and tinyMCE editor, I am validating this form by appending the string "Required" after each field if it is blank, However I am unable to validate the tinyMCE editor, if the editor is blank, I tried something like

我需要验证表单。这个表单有一些下拉菜单和 tinyMCE 编辑器,我通过在每个字段后面附加字符串“Required”来验证这个表单,如果它是空的,但是我无法验证 tinyMCE 编辑器,如果编辑器是空的,我尝试了类似的东西

tinyMCE.get('tinyedotor').getContent();

but no luck.

但没有运气。

here is my fiddle

这是我的小提琴

回答by Viktor

getContent()should work just fine. Your fiddle doesn't contain the form validation code for the editor value, which is quite crucial here. Try this:

getContent()应该工作得很好。您的小提琴不包含编辑器值的表单验证代码,这在这里非常重要。尝试这个:

var editorContent = tinyMCE.get('tinyeditor').getContent();
if (editorContent == '')
{
    // Editor empty
}
else
{
    // Editor contains a value
}

Forked fiddle

分叉的小提琴

Also note you've declared multiple id's for your selectdrop-down.

另请注意,您已idselect下拉列表声明了 multiple 。

Edit:You can get the idof the editor container with the getContainer()method: tinyMCE.get('tinyeditor').getContainer(). Inserting an error message after the editor would then be something like this:

编辑:您可以id使用以下getContainer()方法获取编辑器容器的:tinyMCE.get('tinyeditor').getContainer()。在编辑器之后插入错误消息将是这样的:

$('<span class="error">Editor empty</span>').insertAfter($(tinyMCE.get('tinyeditor').getContainer()));

This, however, will create a new spaneach time the user clicks the submit button, so you'll probably want to have an error message container with a unique idand check if the container already exists before inserting it.

但是,这将span在用户每次单击提交按钮时创建一个新的,因此您可能需要一个具有唯一性的错误消息容器,id并在插入之前检查该容器是否已存在。

Edit 2: Updated fiddle.

编辑 2更新小提琴

回答by Benjamin Piepiora

You can do this to check if the content is empty without parsing html:

您可以这样做来检查内容是否为空,而无需解析 html:

var content = tinymce.get('tinymceEditor').getContent({format: 'text'});
if($.trim(content) == '')
{
   // editor is empty ...
}

回答by Thariama

What you want can be easily done. Her is a link to a fiddlewith my solution.

您想要的都可以轻松完成。她是我的解决方案的一个链接

回答by Abhishek Chandel

Using the getcontent() is the proper way, but what if user enters the space !!??
Here is the complete solution with the RegEX -

使用 getcontent() 是正确的方法,但是如果用户输入空间怎么办????
这是 RegEX 的完整解决方案 -

var content = tinyMCE.get('tinyeditor').getContent(), patt;

            //Here goes the RegEx
            patt = /^<p>(&nbsp;\s)+(&nbsp;)+<\/p>$/g;

            if (content == '' || patt.test(content)) {
                $('.bgcolor').css("border", "1px solid Red")
                return false;
            }
            else {
                $('.bgcolor').removeAttr("style")
                return true;
            }

Note:('.bgcolor') is nothing but a div around the 'tinyeditor' to have the red border when validation occurs.

注意:('.bgcolor') 只不过是围绕 'tinyeditor' 的一个 div,在验证发生时具有红色边框。

回答by buley

getContent()is the way to go. You could just use the tinyMCE.activeEditorobject and call getContent()on that or get the editor instance by id, like you're doing.

getContent()是要走的路。您可以使用该tinyMCE.activeEditor对象并调用该对象getContent()或通过 id 获取编辑器实例,就像您正在做的那样。

It looks like you've got a typo in your id, which is probably causing your issue.

您的 ID 中好像有一个拼写错误,这可能是导致您出现问题的原因。

tinyMCE.get('tinyedotor').getContent();

should probably be:

应该是:

tinyMCE.get('tinyeditor').getContent();