Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Feb 2005
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question word filter help

    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?

    Code:
    <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;
    	}
    }
    
    </script>

  • #2
    Regular Coder martin_narg's Avatar
    Join Date
    Jul 2002
    Location
    Chamonix, France
    Posts
    600
    Thanks
    1
    Thanked 3 Times in 3 Posts
    This should clean things up so to speak. Hopefully will point you in the direction you are looking for.

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <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";
    	checkWords(str);
    }
    </script>
    </head>
    
    <body>
    </body>
    </html>
    Hope this helps

    m_n
    "Cos it's strange isn't it. You stand in the middle of a library and go 'Aaaaaaaaaaaaaaaaggggggghhhhhhh!'
    and everybody just stares at you. But you do the same in an aeroplane, and everybody joins in."
    -Tommy Cooper


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •