View Full Version : word filter help

08-15-2005, 04:08 AM
Hey guys, I want to make this script display a list of the bad words that come up in the alert box rather than simply displaying how many bad words there are. So if the word bloody is typed I want it to show that in the alert box along with any other curse words. Can someone help me out?

<script type="text/javascript">

badWords = ['sod you', 'bollocks', 'bloody'];

function check (f) {
if (!/\S/.test (f.elements.message.value)) {alert ('Please, a message is required.'); return false};
if (!/\S/.test (f.elements.title.value)) {alert ('Please, a message is required.'); return false};

var count = 0, i = 0, pattern, word;
while (word = badWords[i++]) {
pattern = new RegExp ('\\b' + word + '\\b', 'gi');
while (pattern.test(f.elements.message.value)) {count++}
if (count) {
alert ('Shame on you. You have used bad words ' + count + ' times.')
return false;


08-15-2005, 04:39 AM
This should clean things up so to speak. Hopefully will point you in the direction you are looking for.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
function checkWords(s) {
var bad = ["bloody", "sod"];
var rExp = new RegExp("("+bad.join("|")+")", "ig");
if(rExp.test(s)) {
alert("bad word used: "+RegExp.$1);

window.onload = function() {
var str = "hello sodding world";


Hope this helps