简体   繁体   中英

Check for empty text from a rich text editor

How to check for empty text from a rich text editor?

I have a Rich text, similar to this one where I am typing.

By default, the value is set to <br> so, in Java when i check for request.getParameter("desc"); I will get the value as <br>

I want to check for empty string, including any html tags like just <br><hr> etc

Is this possible?

Use a HTML parser like Jsoup .

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. 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.

Assumes that a non-tag < is given as entity &lt; .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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