Solved

Detect a bad words in VBScript

Posted on 2003-11-05
16
581 Views
Last Modified: 2008-03-06
I have this simple codes :

<html>
<head>
<script language="vbscript">

 Dim x(5)

     x(1) = "Profanity word here"
     x(2) = "Profanity word here"
     x(3) = "Profanity word here"
     x(4) = "Profanity word here"
     x(5) = "Gago"

 Dim y

 Sub cmdbtn1_onClick

     If (document.frmX.txt1.value="") Then
     MsgBox "No comments not allowed !"
     End If

     For y = 1 To 5 Step 1

     If (document.frmX.txt1.value=x(y)) Then
     MsgBox "Bad words not allowed !"  
     document.frmX.txt1.value=""    
     End If
     next

 End Sub

</script>
</head>

<body><form name="frmX"><br><br><br>
<font face="arial" size="3" color="black"><b>
Thank you for visiting !</b></font><br>
<font face="arial" size="2"><br>
Pls. Take a moment to sign this guessbook<br>
Always hoping for your next visit thanks.<br>
<br><br>
<textarea name="txt1" cols="30" rows="10"></textarea><br><br>
<input type=submit name="cmdbtn1" value="Sign the Guestbook">
</form>
</body>
</html>

Questions :

1.) It detects a simple bad word like "Profanity word here" but if I change it like "Profanity word here" it doesnt detect it.
     Can anyone show me how can I detect it even if its a lower case ? any methods ?

2.) Can any one also show how to parse the word. Example if the user type in
     "mjkmjProfanity word herelkmm" it will only detect the Profanity word here.

Thanks anyone who can complete the codes will receive addtional 20 pts.
0
Comment
Question by:Rosewell
  • 7
  • 5
  • 2
  • +1
16 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 9691704
   If (ucase(document.frmX.txt1.value)=ucase(x(y)) Then
    MsgBox "Bad words not allowed !"  
     document.frmX.txt1.value=""    
     End If
    next
0
 
LVL 58

Assisted Solution

by:Gary
Gary earned 80 total points
ID: 9691709
Amendment to that

  If ucase(document.frmX.txt1.value)=ucase(x(y)) Then
   MsgBox "Bad words not allowed !"  
     document.frmX.txt1.value=""    
     End If
   next
0
 
LVL 58

Expert Comment

by:Gary
ID: 9691748
On a side note you would be best doing this kinda check serverside, VBScript will only work in IE and the alternative - javascript - can be disabled.  Making it very easy to get past your profanity check.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Rosewell
ID: 9691792
>> VBScript will only work in IE and the alternative - javascript - can be disabled.

     Do you know any other script that can fix this ?

>> Edited by Computer101
 
     What was edited ?

     
0
 

Author Comment

by:Rosewell
ID: 9691801
Do you have an answer on my second question?
0
 
LVL 58

Expert Comment

by:Gary
ID: 9691805
Thats the only two programming options.  Like I said this is best done using ASP at server side after the form has been submitted. e.g.
<%
comment=request("commentfield")
badwords=array("badword1","badword2") ' etc

for count=0 to ubound(badwords)
   comment =  Replace(comment,badwords(count), "", 1, -1, vbTextCompare)
next
%>

And now you have the variable comment with all the bad words taken out.
0
 
LVL 58

Expert Comment

by:Gary
ID: 9691807
Would have thought it obvious what was edited.
0
 

Author Comment

by:Rosewell
ID: 9691829
Can you eliminate in vbscript an extra text for example :

sdfjsdjfBadWordasdasd

It will just detect the "BadWord" not the extra text, thanks.
0
 
LVL 58

Expert Comment

by:Gary
ID: 9691836
For the second question the script just above deals with it.  Or if you want to stay with clientside
then this takes care of both instances.


varStr="mjkmjProfanity word herelkmm"

For y = 1 To 5
     badcheck= instr(ucase(varStr),ucase(x(y)))
     if badcheck>0 then
         MsgBox "Bad words not allowed !"  
         document.frmX.txt1.value=""  
         exit for
     end if
next
0
 

Author Comment

by:Rosewell
ID: 9691858
I need a littile help friend.

I tried to use it but wont work what I want to acomplish is a user input.

     If ucase(document.frmX.txt1.value)=ucase(x(y)) Then
     MsgBox "Bad words not allowed !"  
     document.frmX.txt1.value=""    
     End If

This codes tries and detects a uppercase user input.
Can you add your codes on this codes.

Thanks
0
 
LVL 58

Accepted Solution

by:
Gary earned 80 total points
ID: 9691884
Ok, just replace your whole script with this.

<script language="vbscript">

x=array("Profanity1","Profanity2","Profanity3","Profanity4", "Profanity5")

 Sub cmdbtn1_onClick
     varComment=document.frmX.txt1.value
     If (varComment="") Then
         MsgBox "No comments not allowed !"
     End If

     badcount=0
     For y = 0 To 4
       badcheck= instr(ucase(varComment),ucase(x(y)))
       if badcheck>0 then
           varComment=replace(varComment,x(y),"",1, -1, vbTextCompare)
           badcount=1
       end if
     next
     if badcount=1 then
        MsgBox "No bad words allowed"
        document.frmX.txt1.value=varComment
     end if
   
 End Sub

</script>
0
 
LVL 6

Expert Comment

by:Mike_Metro
ID: 9691910
The following code and be run client or server side.  I agree with GaryC123, this is a task is better suited for the server.  I created two functions below to check bad works and replace bad words.
   
<SCRIPT language=vbscript>
Dim strBadWordPattern
strBadWordPattern = "Badword1|Badword2|BadWord3|etc"
'or if you already have a bad word array
'strBadWordPattern =  = Join(arrBadWords, "|")

Function blnHasProfanity(strText)
  Dim objReg
  set objReg = new RegExp
  objReg.Global = true
  objReg.IgnoreCase  = true
  objReg.Multiline = true
  objReg.Pattern = strBadWordPattern
  blnHasBadWords = objReg.Test(strText)
  Set objReg = nothing
End Function

Function RemoveProfanity(strText)
  Dim objReg
  set objReg = new RegExp
  objReg.Global = true
  objReg.IgnoreCase  = true
  objReg.Multiline = true
  objReg.Pattern = strBadWordPattern
  RemoveProfanity = objReg.Replace(strText, "****")
  Set objReg = nothing
End Function
</SCRIPT>
0
 
LVL 6

Assisted Solution

by:Mike_Metro
Mike_Metro earned 20 total points
ID: 9691915
Sorry one small change
   
<SCRIPT language=vbscript>
Dim strBadWordPattern
strBadWordPattern = "Badword1|Badword2|BadWord3|etc"
'or if you already have a bad word array
'strBadWordPattern =  = Join(arrBadWords, "|")

Function blnHasProfanity(strText)
  Dim objReg
  set objReg = new RegExp
  objReg.Global = true
  objReg.IgnoreCase  = true
  objReg.Multiline = true
  objReg.Pattern = strBadWordPattern
  blnHasProfanity = objReg.Test(strText)
  Set objReg = nothing
End Function

Function RemoveProfanity(strText)
  Dim objReg
  set objReg = new RegExp
  objReg.Global = true
  objReg.IgnoreCase  = true
  objReg.Multiline = true
  objReg.Pattern = strBadWordPattern
  RemoveProfanity = objReg.Replace(strText, "****")
  Set objReg = nothing
End Function
</SCRIPT>
0
 
LVL 23

Assisted Solution

by:Saqib Khan
Saqib Khan earned 20 total points
ID: 9691962
Here is an Example with Client Side javaScript.


<html>
<head>
<Script>
function filter() {
// Bad word's Seperated by the Coma
invalids = new Array("!","#","%")
var h = document.f1.t1.value
for(i=0; i<=h.length; i++) {

if(h.indexOf(invalids[i])!=-1) {

alert("detected")
return false;
}

}


}
</Script>
</head>
<title> Test</title>
<body>
<form name="f1">
<textarea rows=5- cols=50 name="t1"></textarea>
<input type="button" onClick="filter()" value="Filter">
</form>

</body>
</html>


Compatible with All Strandard Browser's.
its always a Good Idea to Test your Data on Client before Processing it to Server, this way we can Save some Load Time on Server.
0
 

Author Comment

by:Rosewell
ID: 9692174
Increased Pts :)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Bypass cross origin issues on development site 3 95
Questions about INCLUDE FILES 2 42
JS does not refresh 6 38
Extracting nodes  using classical ASP 3 23
Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question