473,321 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

Word filter by length of the word??

29
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


Expand|Select|Wrap|Line Numbers
  1. <HTML>
  2.  
  3. <HEAD>
  4.  
  5. <TITLE>Test Input </TITLE>
  6.  
  7.  
  8.  
  9.  
  10.  
  11. <!--BEGIN WORD FILTER JAVASCRIPT-->
  12.  
  13. <script language="JavaScript1.2">
  14.  
  15.  
  16.  
  17.  
  18.  
  19. var special_words_arr=new Array("cheer","bloody","war","terror");
  20.  
  21. var special_alert_arr=new Array;
  22.  
  23. var special_alert_count=0;
  24.  
  25.  
  26.  
  27. function reset_alert_count()
  28.  
  29. {
  30.  
  31. special_alert_count=0;
  32.  
  33. }
  34.  
  35.  
  36.  
  37. function validate_user_text()
  38.  
  39. {
  40.  
  41. reset_alert_count();
  42.  
  43. var compare_text=document.form1.user_text.value;
  44.  
  45. for(var i=0; i<special_words_arr.length; i++)
  46.  
  47. {
  48.  
  49.   for(var j=0; j<(compare_text.length); j++)
  50.  
  51.   {
  52.  
  53.    if(special_words_arr[i]==compare_text.substring(j,(j+special_words_arr[i].length)).toLowerCase())
  54.  
  55.    {
  56.  
  57.     special_alert_arr[special_alert_count]=compare_text.substring(j,(j+special_words_arr[i].length));
  58.  
  59.     special_alert_count++;
  60.  
  61.    }
  62.  
  63.   }
  64.  
  65. }
  66.  
  67. var alert_text="";
  68.  
  69. for(var k=1; k<=special_alert_count; k++)
  70.  
  71. {
  72.  
  73.   alert_text+="\n" + "(" + k + ")  " + special_alert_arr[k-1];
  74.  
  75. }
  76.  
  77. if(special_alert_count>0)
  78.  
  79. {
  80.  
  81.   alert(" these words are not valide cheer,bloody,war,terror");
  82.  
  83.   document.form1.user_text.select();
  84.  
  85. }
  86.  
  87. else
  88.  
  89. {
  90.  
  91.    alert("well done no special words used");
  92.  
  93. }
  94.  
  95. }
  96.  
  97.  
  98.  
  99. function select_area()
  100.  
  101. {
  102.  
  103. document.form1.user_text.select();
  104.  
  105. }
  106.  
  107.  
  108.  
  109. window.onload=reset_alert_count;
  110.  
  111.  
  112.  
  113. </script>
  114.  
  115. <!--END WORD FILTER JAVASCRIPT-->
  116.  
  117.  
  118.  
  119. </HEAD>
  120.  
  121. <BODY>
  122.  
  123.  
  124.  
  125. <form name="form1" method="post" action="your_post_address.asp">
  126.  
  127. <center><font face="Times New Roman" size="6pt" color="#606060"><b><i>Word Filter</i></b></font></center>
  128.  
  129. <table><tr><td></td></tr></table>
  130.  
  131. <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>
  132.  
  133. <table><tr><td></td></tr></table>
  134.  
  135. <center><input type="button" style="background:#EFEFEF; border:2 solid #808080; width:100%; cursor:pointer" value="Submit" onclick="validate_user_text();"></center>
  136.  
  137. </form></form>
  138.  
  139.  
  140.  
  141. </BODY>
  142.  
  143. </HTML> 
  144.  
Jun 7 '10 #1
3 2658
gits
5,390 Expert Mod 4TB
this could be simplyfied with the following for example:

Expand|Select|Wrap|Line Numbers
  1. var specialWords = {
  2.     "cheer": 1, 
  3.     "bloody": 1, 
  4.     "war": 1, 
  5.     "terror": 1
  6. };
  7.  
  8. var s = 'foo foo bloody-mary terror\nmy test text';
  9.  
  10. function checkText(s) {
  11.     var check = true;
  12.  
  13.     var wordList = s.split(/[ |\n]/);
  14.  
  15.     var forbiddenWordsInText = [];
  16.  
  17.     for (var i = 0, l = wordList.length; i < l; ++i) {
  18.         var word = wordList[i];
  19.  
  20.         if (word in specialWords) {
  21.             forbiddenWordsInText.push(word);
  22.         }
  23.     }
  24.  
  25.     if (forbiddenWordsInText.length > 0) {
  26.         check = false;
  27.         alert(forbiddenWordsInText);
  28.     }
  29.  
  30.     return check;
  31. }
  32.  
  33. checkText(s);
  34.  
it uses a JavaScript object for the special words so that an easy look up is possible and therefor it just needs one loop through the text which was splitted according to whitespaces and linebreaks ... you might simply test it in the firebug console ...

kind regards

PS: those check is clientside a bit useless when JavaScript is turned of or the user just modifies the script which easily could be done ... so don't only rely on this and do a serverside check when this would be a critical requirement.
Jun 7 '10 #2
lisa007
29
this what i had using your code but as soon you load the page it simples pop up alert box saying terror then it no longer works

Expand|Select|Wrap|Line Numbers
  1. <HTML>
  2. <HEAD>
  3. <TITLE>Test Input </TITLE>
  4.  
  5.  
  6. <!--BEGIN WORD FILTER JAVASCRIPT-->
  7. <script language="JavaScript1.2">
  8.  
  9.  
  10.  
  11.  
  12.  var specialWords = {
  13.        "cheer": 1, 
  14.     "bloody": 1, 
  15.      "war": 1, 
  16.       "terror": 1
  17.    };
  18.  
  19.    var s = 'foo foo bloody-mary terror\nmy test text';
  20.  
  21.   function checkText(s) {
  22.     var check = true;
  23.  
  24.       var wordList = s.split(/[ |\n]/);
  25.  
  26.      var forbiddenWordsInText = [];
  27.  
  28.       for (var i = 0, l = wordList.length; i < l; ++i) {
  29.            var word = wordList[i];
  30.  
  31.          if (word in specialWords) {
  32.             forbiddenWordsInText.push(word);
  33.           }
  34.      }
  35.  
  36.     if (forbiddenWordsInText.length > 0) {
  37.         check = false;
  38.         alert(forbiddenWordsInText);
  39.     }
  40.  
  41.    return check;
  42.  }
  43.  
  44.  checkText(s);
  45.  
  46.  
  47.  
  48.  
  49. </script>
  50. <!--END WORD FILTER JAVASCRIPT-->
  51.  
  52. </HEAD>
  53. <BODY>
  54.  
  55. <form name="form1" method="post" action="your_post_address.asp">
  56. <center><font face="Times New Roman" size="6pt" color="#606060"><b><i>Word Filter</i></b></font></center>
  57. <table><tr><td></td></tr></table>
  58. <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>
  59. <table><tr><td></td></tr></table>
  60. <center><input type="button" style="background:#EFEFEF; border:2 solid #808080; width:100%; cursor:pointer" value="Submit" onClick="validate_user_text();"></center>
  61. </form></form>
  62.  
  63. </BODY>
  64. </HTML>
  65.  
Jun 7 '10 #3
gits
5,390 Expert Mod 4TB
of course ... you just copied and pasted the script into your page ... you would need to drop the function call and adapt your onclick handler ...
Jun 7 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Lord Merlin | last post by:
Can someone please help me with a good swearword filter, or assist me with my current one? This is the code I have currently, but it doesn't always work. I can do the same test, with the same...
0
by: xl4-w | last post by:
Hi There, Does anyone know where to find a good working MS Word-tag filter, or how to build this.
1
by: RichPu | last post by:
Hi Everyone, I have word 97 and word 2000 installed on the same machine, I need to open word using createobject("word.application") is there a way of choosing which version access will open? at...
1
by: Sakharam Phapale | last post by:
Hi All, I am working on Text editor. We are using Microsoft Word object for Spelling checking and correcting. Now I want to add new word in Word dictionary using Word object. Can anyone guide...
1
by: Franck | last post by:
Hi, Tryin to open a generated word doc with Word and not in IE. Lookin samples, I tried this without success : Response.ContentType = "application/ms-word"...
3
by: bay_dar | last post by:
Hi my dirty word filter function has a problem, it is over restrictive. For instance the word "analysis" is ok, but the word "anal" is not. Any advice? code below: Public Function...
1
by: deepmalahallale | last post by:
i m developing a tool , in which i want to search date of birth from word (.doc) file and store it in access database . can any one help me for this plz code for searching a perticular word from ms...
0
by: abhilash12 | last post by:
is there any searchengine using with java for find word in ms word file pls help me
1
by: devsri79 | last post by:
How to find the word in the word document by using the c#
0
by: alivip | last post by:
I write code to get most frequent words in the file I won't to implement bigram probability by modifying the code to do the following: How can I get every Token (word) and ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.