Nithy
Nithy

Reputation: 23

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?

Upvotes: 2

Views: 2939

Answers (2)

BalusC
BalusC

Reputation: 1109322

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 e.g. a <script> in your text area. You were also checking on that, right?

Upvotes: 4

Joop Eggen
Joop Eggen

Reputation: 109603

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

Upvotes: 1

Related Questions