Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

RegEx to validate textarea and prevent spambots

Posted on 2008-06-26
8
Medium Priority
?
757 Views
Last Modified: 2011-09-20
I am getting spambots on my form and have done some form filtering but a big thing I am getting are URLs in my textarea form field. How would I code my form processing page using regular expressions to validate there are no URLs (i.e. - http://) in my textarea?

Example of what I need in bad code form:

commentsBox=request.form("comments")

regex = "{http://}"

if commentsBox contains regex then
WriteToFile "webcsv\myform_spam.csv", contents, True
else
WriteToFile "webcsv\myform.csv", contents, True
end if

Thanks!

~Aus2Srq
0
Comment
Question by:Aus2Srq
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 16

Expert Comment

by:ThinkPaper
ID: 21877070
you're just checking http:// URLs, nothing else?

you've got several variations to consider.. below's a good chunk of regex's to consider..

http://www.truerwords.net/articles/ut/urlactivation.html
0
 
LVL 16

Expert Comment

by:ThinkPaper
ID: 21877360
also.. what type of form is this? is it like a registration form?

considering using tools like CAPTCHA images to eliminate spam registration..
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 21877878

<%
Set regEx = New RegExp
regEx.Global = False
sourcestring = "your source string"
regEx.Pattern = ".*https?//"
Set Matches = regEx.Execute(sourcestring)
If Matches.Count > 0 then
 ' do something
Else
 ' do something else
End If
%>

Open in new window

0
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!

 
LVL 27

Expert Comment

by:ddrudik
ID: 21877888
One more change:
<%
Set regEx = New RegExp
regEx.Global = False
regEx.IgnoreCase = True
sourcestring = "your source string"
regEx.Pattern = ".*https?//"
Set Matches = regEx.Execute(sourcestring)
If Matches.Count > 0 then
 ' do something
Else
 ' do something else
End If
%>

Open in new window

0
 

Author Comment

by:Aus2Srq
ID: 21878792
Using your code as a base, I did a Google search and modified the code a bit as I don't care if I know the number of patterns found, I just need to know true or false I did the following.

However, it's failing to find the pattern and still submitting the form as normal. Do I need to change my variable to a string to search?

Thanks!

~A2S
commentsBox = request.form("comments")
 
Set regEx = New RegExp
With regEx
.Pattern = ".*https?//"
.IgnoreCase = True
.Global = False
End With
foundPattern = regEx.Test(commentsBox)
 
If foundPattern Then
  WriteToFile "webcsv\myform_spam.csv", contents, True
else
  WriteToFile "webcsv\myform.csv", contents, True
End If
Set regEx = nothing

Open in new window

0
 
LVL 27

Accepted Solution

by:
ddrudik earned 2000 total points
ID: 21878991
I forgot the colon in the pattern, either revision would work with the new pattern below.  Note that you cannot test as you did, once the FoundPattern is created as a regEx.Test object it exists.  The best you can do is test if Matches.Count exists which reports false if it is equal to 0.
<%
Set regEx = New RegExp
regEx.Global = False
regEx.IgnoreCase = True
sourcestring = "http://www.yahoo.com/"
regEx.Pattern = ".*https?://"
Set Matches = regEx.Execute(sourcestring)
If Matches.Count then
  Response.Write "SPAM"
Else
  Response.Write "NOT SPAM"
End If
%>

Open in new window

0
 

Author Closing Comment

by:Aus2Srq
ID: 31471076
Thank you very much ddrudik! It works perfectly.

On a side note, could you leave a last comment on how I would add an or in the regex? Such as looking for "http://" or "www.*.com" for instance.

Thanks again!

~Aus2Srq
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 21879826
Thanks for the question and the points.
<%
Set regEx = New RegExp
regEx.Global = False
regEx.IgnoreCase = True
sourcestring = "test www.yahoo.com test"
regEx.Pattern = "(?:.*https?://)|(?:.*www\..*\.com)"
Set Matches = regEx.Execute(sourcestring)
If Matches.Count then
  Response.Write "SPAM"
Else
  Response.Write "NOT SPAM"
End If
%>

Open in new window

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

722 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