VBScript form spam blocker

I don’t do a great deal of classic ASP any more, but today I had to knock up a quick function to try and filter out those annoying form spammers. You know the ones, feedback forms submitted with just URLs pointing to pr0n and viagra sites!

Anyway, I came up with a function that counts URLs in a block of text –

Function UrlCount(text)
dim strRegex
strRegex = "(https?://)?(([0-9a-z_!~*'().&=+$%-]+: )?
[0-9a-z_!~*'().& =+$%-]+@)?(([0-9]{1,3}.){3}[0-9]{1,3}|
([0-9a-z_!~*'()-]+.)*([0-9a-z] [0-9a-z-]{0,61})?[0-9a-z].[a-z]
{2,6}) (:[0-9]{1,4})?((/?)|(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)"

'Prepare a regular expression object
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Global = True
myRegExp.Pattern = strRegex

Set myMatches = myRegExp.Execute(text)
UrlCount = myMatches.count
Set myMatches = nothing
Set myRegExp = nothing
End Function

Thanks to this url regex I had it sorted in no time 🙂

Usage –

If UrlCount(variablecontainingfieldvaluetocheck) > 1 then
' you could use 2+ above if you don't want to be so strict
' do something nasty like drop a 404
End If

Anyway, let me know if you find it useful or have any suggestions for improvement.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: