Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 605
  • Last Modified:

Detect a bad words in VBScript

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
Rosewell
Asked:
Rosewell
  • 7
  • 5
  • 2
  • +1
4 Solutions
 
GaryCommented:
   If (ucase(document.frmX.txt1.value)=ucase(x(y)) Then
    MsgBox "Bad words not allowed !"  
     document.frmX.txt1.value=""    
     End If
    next
0
 
GaryCommented:
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
 
GaryCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
RosewellAuthor Commented:
>> 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
 
RosewellAuthor Commented:
Do you have an answer on my second question?
0
 
GaryCommented:
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
 
GaryCommented:
Would have thought it obvious what was edited.
0
 
RosewellAuthor Commented:
Can you eliminate in vbscript an extra text for example :

sdfjsdjfBadWordasdasd

It will just detect the "BadWord" not the extra text, thanks.
0
 
GaryCommented:
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
 
RosewellAuthor Commented:
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
 
GaryCommented:
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
 
Mike_MetroCommented:
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
 
Mike_MetroCommented:
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
 
Saqib KhanSenior DeveloperCommented:
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
 
RosewellAuthor Commented:
Increased Pts :)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 7
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now