简体   繁体   English

从RTF编辑器中检查空文本

[英]Check for empty text from a rich text editor

How to check for empty text from a rich text editor? 如何通过RTF编辑器检查空文本?

I have a Rich text, similar to this one where I am typing. 我有一个RTF文字,类似于我在其中键入的文字。

By default, the value is set to <br> so, in Java when i check for request.getParameter("desc"); 默认情况下,该值设置为<br>因此,在Java中,当我检查request.getParameter("desc");时,该值request.getParameter("desc"); I will get the value as <br> 我将得到的值为<br>

I want to check for empty string, including any html tags like just <br><hr> etc 我想检查空字符串,包括任何HTML标记,例如<br><hr>

Is this possible? 这可能吗?

Use a HTML parser like Jsoup . 使用类似Jsoup的HTML解析器。

String text = Jsoup.parse(html).text();

if (text.isEmpty()) {
    // No text.
}

Additional advantage is that it can also help you with sanitizing HTML to avoid XSS attacks when a malicious enduser enters eg a <script> in your text area. 另一个好处是,当恶意的最终用户在文本区域输入例如<script>时,它还可以帮助您清理 HTML,避免XSS攻击。 You were also checking on that, right? 您也在对此进行检查,对吗?

Maybe simple-minded, but just remove all tag words (includes image and button). 也许头脑简单,但只需删除所有标记 (包括图像和按钮)即可。

public static boolean isEmpty(String text) {
    return text.replaceAll("<[^>]+>", "").trim().isEmpty();
}

Maybe with a replaceAll removal of whitespace and line breaks. 也许用replaceAll删除空格和换行符。

Assumes that a non-tag < is given as entity &lt; 假设非标签<被指定为实体&lt; .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM