How To Filter Invalid(vulgar) words in JSP?
when data was entered to a textbox it must not contain vulgar or bad words.The event is processed before data is saved and posted.
Do I need to specify each word or there are some shortcuts for it?
Can you give me a concrete example..tnx a lot!!!

Dani AI

Generated

Good thread — you’re on the right track asking for a pre-save check. is correct that a deny-list is the simplest start, and is right client-side checks help UX. But as showed, simple examples miss obfuscation (e.g., f***k), so do the real check server-side (Servlet or JSP filter) and use JS only for immediate feedback.

Practical, robust approach (summary):

  • Keep the bad-words list outside code (file/DB) and load into a HashSet for O(1) lookups.
  • Normalize input: Unicode normalizer to strip accents, lowercase, collapse repeated characters, and replace common “leet” substitutions (4→a, 3→e, @→a, $→s, etc.).
  • Match whole words (word boundaries) to avoid false positives like class matching ###. For obfuscated forms, either deobfuscate first or use patterns that allow non-letter separators between characters.
  • For large lists or many checks, use an Aho–Corasick automaton (multi-pattern search) or a single precompiled regex. Precompile Patterns once and reuse them.

Tiny Java example (conceptual — drop into a utility class):

String normalized = Normalizer.normalize(input, Normalizer.Form.NFKD)
                    .replaceAll("\\p{M}", "")
                    .toLowerCase()
                    .replaceAll("(.)\\1{2,}", "$1$1")
                    .replaceAll("[@4]", "a").replaceAll("3", "e").replaceAll("1", "i").replaceAll("0", "o").replaceAll("\\$", "s");

Pattern p = Pattern.compile("\\b(" + String.join("|", badWordsSet) + ")\\b", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
boolean found = p.matcher(normalized).find();

Deployment notes and cautions:

  • Never rely on client-side filtering alone. Put the check in a Servlet Filter or before DB write.
  • Use a safe allowlist for known legitimate words to reduce false positives.
  • Log and queue flagged content for human review rather than auto-deleting in borderline cases.
  • For production-scale moderation or context-aware decisions, consider a dedicated moderation service or ML-based API.

This gives a practical path from the simple list idea in the thread to a production-ready server-side strategy.

Recommended Answers

All 6 Replies

usually you have a list of words and check each word against that list.
There are more advanced systems that do full lexical analysis to also take context into account but you're not ready for those.

can you please giv me a brief example?tnx...

jUST wanna share you what I've got due to extensive web research:) ..Visit this site, it really fits well to my needs:


knowledge is learned through acceptance

If you happy with JavaScript why not...

>>Visit this site, it really fits well to my needs:
Ok, so it won't accept the word "bloody" but it will accept "f***k". Some crappy filter that is :) bloody is not a naughty word but the other is.

Well you need to maintain the array of rude words, they provided only 3 words in their example

var swear_words_arr=new Array("bloody","war","terror");

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.