Click to See Complete Forum and Search --> : Help required to improve error checking


kessa
06-18-2006, 06:23 PM
Hi All,

I've got the following bit of code which I use to ensure a name doesn't contain a number or an @ symbol:


strName = Request.Form("name")
strName = LCase(strName) ' Add if you want your tests to be case-insensitive!
If InStr(strName, "n/a") > 0 OR InStr(strName, "@") > 0 OR InStr(strName, "1") > 0 OR InStr(strName, "2") > 0 OR InStr(strName, "3") > 0 OR InStr(strName, "4") > 0 OR InStr(strName, "5") > 0 OR InStr(strName, "6") > 0 OR InStr(strName, "7") > 0 OR InStr(strName, "8") > 0 OR InStr(strName, "9") > 0 OR InStr(strName, "0") > 0 Then
update_fail=1
End If


At the moment it works fine, but I was just curious to know if there may be a more streamlined way of doing this (perhaps some kind of loop?) - my main concern is that I use the same method in other form fields (to help detect spam in form comments, etc) and some of these are now getting quite long (20 or so words to look out for), so I seems a bit of waste to keep repeating "OR InStr(strName, "whatever") > 0" if there is a way I could perhaps just declare the words on their own, and then loop through them.

Also, on a seperate issue, I also wondered if it was possible to ensure that the user has entered at least "X" number of characters into a form field (this isn't related to the issue above and so would need to be a seperate function)

For example - if they have entered something into the "telephone" field, then I want to make sure that it is at least 5 characters, etc

Thanks
Kessa

kessa
06-20-2006, 11:43 AM
anyone?

Cheers
K

lmf232s
06-20-2006, 12:16 PM
I believe this is kind of what you are looking for.

Function Validate(obj)
Dim Accepted
Dim i, c

Accepted = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

For i = 1 to len(obj)
c = mid(obj, i, 1)
if ( Instr(Accepted, c) = 0 ) then
Validate = False
exit Function
end if
Next
Validate = True
End Function

russell
06-20-2006, 12:32 PM
or use regular expressions...

<%
Function isValidName(strInput)
Dim objRegExpr
Set objRegExpr = new RegExp

objRegExpr.Pattern = "[0-9@]" '' match numbers or an @ symbol
objRegExpr.Global = True
objRegExpr.IgnoreCase = True

isValidName = Not objRegExpr.Test(strInput)

Set objRegExpr = Nothing
End Function

If Not isValidName(strName) Then
update_fail=1
End If

'Response.Write isValidName("russell") '' True
'Response.Write isValidName("ru4ssell") '' False
'Response.Write isValidName("russell@") '' False
%>

kessa
06-20-2006, 04:04 PM
Hi Guys,

Firstly, thanks to you both - I'll incorporate one of these for the example used.

However, I guess that I may have also used a bad example in the original post (sorry :o !) as the solutions provided don't quite address what I'm trying to do (at least, not if I follow correctly)

is a way I could perhaps just declare the words on their own, and then loop through them.

What I would like to do (in some cases) is set up a list of words to look out for (so not just numbers / letters) in an attempt to help block spam.

So for example, I want to be able to set up something (I'm guessing an array perhaps?) and loop through it looking for any prohibited words:

rudeword1
rudeword2
rudeword3

If what they have entered matches one of the rudewords, then deliver an error message.

Hope that makes more sense?

Also, any idea how I can address the second issue (ensuring that they have entered at least X number of characters? (This should be a seperate function to the one above)


Kessa

lmf232s
06-20-2006, 04:25 PM
maybe something like

Function Validate(obj)
Dim InValid : InValid = Array("Larryf", "RussellB", "ChrisS", "SPAM", "@")

For i = 0 To UBound(InValid)
If InStr(lcase(obj), lcase(InValid(i))) > 0 Then
Validate = False
Exit Function
End if
Next

Validate = True
End Function

russell
06-20-2006, 05:27 PM
The best part is all the fun you can have making that list of rudewords :D

kessa
06-21-2006, 12:04 PM
Hi Russell - most definatly... should keep me quite for a while!

Update:
arrrghhh... (sorry, just needed a little outburst - I feel better now :D )

This worked a treat but I'm still getting a load of spam
(rather than calling these "people" names I think I'll just reverse the process and call them all of the words I'm trying to block... and then some!)

Anyway, I still need to find a way to ensure that they have entered at least X number of characters (and perhaps as an extra safeguard) strip any spaces (so that they can't just enter X number of spaces) - I've got a bit of javascript which does this but they are getting around it so I guess they have turned JS off.

Any ideas?

Thanks for keeping me sane(ish)
Kessa

russell
06-21-2006, 12:08 PM
we can check for anything we want. if we add a line or 2 to lmf232s example...

Function Validate(obj)
Dim InValid : InValid = Array("Larryf", "RussellB", "ChrisS", "SPAM", "@")

If len(obj) < 10 Then
Validate = False
Exit Function
End If

For i = 0 To UBound(InValid)
If InStr(lcase(obj), lcase(InValid(i))) &gt; 0 Then
Validate = False
Exit Function
End if
Next

Validate = True
End Function

kessa
06-21-2006, 12:16 PM
spot on! (and nice and simple too ;) )

I'll add that to the form and will let you know how I get on

Thanks - you're (both) a star!
Kessa