Form Validation w/ASP?

07-31-2002, 05:20 PM

I was wondering if there was a way to quickly check if a submitted form field had anything other than letter/numbers in it. By using the following statement, I can check for each special character, but that would require a seperate test for each special character or symbol. I can also use a case statement.....but is there a faster way than using nested IF's or long case statements?

Here's an example of what I mean:

Dim Form_Field
Form_Field = Request("Form_Field")

If InStr(Form_Field,"@")=1 Then
'invalid input
ElseIf InStr(Form_Field,"!")=1 Then
'invalid input
ElseIf InStr(Form_Email,"#")=1 Then
'invalid input

....and so on, for each non number/letter.
End If

I guess what i'm asking is if there is a way to parse the entire string at once and detect anything that isn't a letter or number, then redirect them with an error message..?

Thanks in advance for any help.

07-31-2002, 06:10 PM
The only way to avoid checking each character in the string is to
use a regex object and a regular expression check. If I had to
loop and check characters, I would do it by taking each character
in Form_Field, and comparing its ascii value to the range of
character codes that were acceptable (48-57 for numbers, 65-90
for uppercase letters, 97-122 for lowercase letters).

07-31-2002, 06:29 PM
To do what Thinker suggested, I whipped up a little check. It should be faster then using many nested If statements, plus it will terminate once an invalid character is found.'Initialize to be true
bolGood = true

'look at each character in the string
for x = 1 to Len(request("txtTest"))

'Convert this character into an ASCII code
num = asc(mid(request("txtTest"),x,1))

'If bolGood still = True then check if the
'value is with the required ASCII ranges.

If (num >= 97 AND num <= 122) OR _
(num >= 65 AND num <= 90) OR _
(num >= 48 AND num <= 57) Then
bolGood = True
'Terminate, since an invalid character
'has been found
bolGood = False
Exit For
end if

nextThen, to check the value for a valid form input:if Not bolGood then
response.write "Invalid entry"
response.write "Valid entry"
end if

modified the If statement

07-31-2002, 07:09 PM
Here's a client side JavaScript I came up with to accomplish the exact same thing. The big plus here is that, obviously, it's done client side -- meaning that the "strain" can be taken off of the server and placed on the client's machine:<HTML>


var numcheck = "0123456789";
var lowercheck = "abcdefghijklmnopqrstuvwxyz";

function Check(check,v)
for (z=0;z<check.length;z++)
if (v==check.substr(z,1))
return true;

return false;

function CheckForm(){

var v = document.frmCheck.txtTest.value;

if (v.length == 0){
alert ('You must enter something.');
return false;}

var bolGood = false;

for (x=0;x<v.length;x++){
bolGood = ((Check(lowercheck,v.substr(x,1)))||

if (!bolGood)
alert ('You have entered one or more invalid characters.');

return bolGood;




Enter a-z, A-Z, 0-9<BR>
<FORM NAME="frmCheck" METHOD=POST ACTION=check.asp onSubmit="return CheckForm();">
<INPUT TYPE=Submit VALUE=Submit>



07-31-2002, 08:28 PM
Very Cool. Thanks a bunch. I'm gonna go with the ASP solution, just in case the user has javascript disabled. That's going to come in very handy for other validation checks i need to do as well. Thanks again.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum