Solved

Detect a bad words in VBScript

Posted on 2003-11-05
16
557 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
Comment Utility
   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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:Rosewell
Comment Utility
>> 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
Comment Utility
Do you have an answer on my second question?
0
 
LVL 58

Expert Comment

by:Gary
Comment Utility
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
Comment Utility
Would have thought it obvious what was edited.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:Rosewell
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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:adilkhan
adilkhan earned 20 total points
Comment Utility
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
Comment Utility
Increased Pts :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now