Lisa23 Posted June 7, 2010 Share Posted June 7, 2010 i have a word filter script but the problem is that it can be overpassed easy what i want is to count exacly the letter of the word cz for example the word cheer is banned but not the word cheerleader but the filter validates cheerleader as if it was cheer how can i get to count the words exacly <HTML> <HEAD> <TITLE>Test Input </TITLE> <!--BEGIN WORD FILTER JAVASCRIPT--> <script language="JavaScript1.2"> var special_words_arr=new Array("cheer","bloody","war","terror"); var special_alert_arr=new Array; var special_alert_count=0; function reset_alert_count() { special_alert_count=0; } function validate_user_text() { reset_alert_count(); var compare_text=document.form1.user_text.value; for(var i=0; i<special_words_arr.length; i++) { for(var j=0; j<(compare_text.length); j++) { if(special_words_arr[i]==compare_text.substring(j,(j+special_words_arr[i].length)).toLowerCase()) { special_alert_arr[special_alert_count]=compare_text.substring(j,(j+special_words_arr[i].length)); special_alert_count++; } } } var alert_text=""; for(var k=1; k<=special_alert_count; k++) { alert_text+="\n" + "(" + k + ") " + special_alert_arr[k-1]; } if(special_alert_count>0) { alert(" these words are not valide cheer,bloody,war,terror"); document.form1.user_text.select(); } else { alert("well done no special words used"); } } function select_area() { document.form1.user_text.select(); } window.onload=reset_alert_count; </script> <!--END WORD FILTER JAVASCRIPT--> </HEAD> <BODY> <form name="form1" method="post" action="your_post_address.asp"> <center><font face="Times New Roman" size="6pt" color="#606060"><b><i>Word Filter</i></b></font></center> <table><tr><td></td></tr></table> <textarea rows="3" cols="40" name="user_text" style="border:2 solid #808080; font-family:verdana,arial,helvetica; font-weight:normal; font-size:10pt" onclick="select_area()">Enter your text here...</textarea> <table><tr><td></td></tr></table> <center><input type="button" style="background:#EFEFEF; border:2 solid #808080; width:100%; cursor:pointer" value="Submit" onclick="validate_user_text();"></center> </form></form> </BODY> </HTML> Quote Link to comment Share on other sites More sharing options...
haku Posted June 7, 2010 Share Posted June 7, 2010 Try this. I'm not 100% sure the regex will work, because the javascript regex engine isn't as powerful as the PHP one I'm used to, but try it out to see if it works. var word1 = "cheer" var word2 = "cheerful" if(word1.match(/^cheer$/)) { alert("cheer is a match for cheer"); } else { alert("cheer is not a match for cheer"); } if(word2.match(/^cheer$/)) { alert("cheerful is a match for cheer"); } else { alert("cheerful is not a match for cheer"); } Quote Link to comment Share on other sites More sharing options...
brianlange Posted June 7, 2010 Share Posted June 7, 2010 You don't really need to use regular expressions if you are comparing exact word matches. Just convert the user entered text into an array using the split function. Loop through this array and see if it exists within the array of forbidden words. You'll have to add a in_array function to Array.prototype because unlike php this function isn't native to javascript. Use toLowerCase if you want the search to be case insensitive. Here's the code. <!--BEGIN WORD FILTER JAVASCRIPT--> <script language="JavaScript1.2"> Array.prototype.in_array = function(p_val) { for(var i = 0, l = this.length; i < l; i++) { if(this == p_val) { return true; } } return false; } var special_words = ["cheer","bloody","war","terror"]; var special_alert = []; var special_alert_count=0; function reset_alert_count() { special_alert_count=0; } function validate_user_text() { reset_alert_count(); var compare_text=document.form1.user_text.value; var user_text = compare_text.split(" "); var x = 0; for (i in user_text) { if (typeof user_text != "string") continue; if (special_words.in_array(user_text.toLowerCase())) { special_alert[x] = user_text; x++; } } if (special_alert.length > 0) { alert('The following words are forbidden ' + special_alert.join(", ")); } } </script> Quote Link to comment Share on other sites More sharing options...
ignace Posted June 7, 2010 Share Posted June 7, 2010 You don't really need to use regular expressions if you are comparing exact word matches. The thing is that in order to create a good filter you'll have to think ahead of possible creative words like: $hit -- hit is not a bad word, shit however is. s.h.i.t. -- the same applies So the filter has to be smart enough to block bad words in any form. Personally I would consider an approach like: 1. convert known symbols for their letter-counterparts ($ -> s) 2. replace rare symbols in a word with ? and perform regex to find any matches with bad words (f*ck -> f?ck -> found:fuck) 3. remove any symbols that have a rare usage in phrases and search for matches ... Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 7, 2010 Share Posted June 7, 2010 Just convert the user entered text into an array using the split function. Loop through this array and see if it exists within the array of forbidden words. That might make sense if the user entered text is of relatively short length. If it is quite large, looping through each and every word doesn't seem too efficient. Here's a quick exampe of building a regex pattern for each banned word. Most importantly, it uses the word boundry placeholder \b so it will only find exact matches. I also made it case-insensitive (Note I used \\b in the code below to overcome JS's escaping of \) <html> <head> <script type="text/javascript"> var special_words_arr=new Array("cheer","bloody","war","terror"); function checkForSpecialWords(inputStr, specialsAry) { var pattern; for(var i=0; i<specialsAry.length; i++) { pattern = new RegExp('\\b'+specialsAry[i]+'\\b', 'gi'); if(inputStr.match(pattern)) { return true; } } return false; } function checkInput() { var userInput = document.getElementById('input').value; if(checkForSpecialWords(userInput, special_words_arr)) { alert('Specials were used'); } else { alert('Specials were NOT used'); } return; } </script> </head> <body> <textarea name="input" id="input"></textarea> <button onclick="checkInput();" >Check Input</button><br /> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.