[thelist] checking existence of a form element with VBScript
Ken Schaefer
ken at adOpenStatic.com
Wed Jun 16 20:54:40 CDT 2004
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: "Joel D Canfield" <joel at spinhead.com>
Subject: [thelist] checking existence of a form element with VBScript
Perhaps I'm slipping into senility. I felt certain I'd asked this here
before, and got it answered. Not only do I not remember the definitive
answer, I can't find in searching thelist archives.
In a form response page, how do I test for the existence of a form field
value being passed from the form? I don't care what the value is, I just
want to know if there *is* one. I've played with IsNull and IsEmpty,
played with > comparison strings, and nothing seems to give reasonable
results.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If Len(Request.Form("element")) > 0
-or-
If CStr(Request.Form("element")) <> ""
the code snippet below will allow you to test for the utility of things like
"is Nothing" or isNull or isEmpty. In this case, a collection's element is
not an object, so is Nothing will not work. Likewise, it's not NULL (there's
nothing there to be NULL), and isEmpty is used to test for an uninitialised
variable.
Since the TypeName returns iStringList, I would tend to check to see if the
collection element = ""
Cheers
Ken
<html>
<head>
<title></title>
</head>
<body>
<p>
<a href="requestformtest.asp">Start Over</a>
</p>
<form method="post" action="requestformtest.asp">
Fill in: <input type="text" name="txt1"
value="<%=Request.Form("txt1")%>" /><br />
Leave Empty<input type="text" name="txt2"
value="<%=Request.Form("txt2")%>" /><br />
<input type="submit" name="btnSubmit" value="Submit" /><br />
</form>
<%
If LCase(Request.ServerVariables("HTTP_Method")) = "post" then
Response.Write("Testing txt1<br />" & vbCrlf)
If Request.Form("txt1") is Nothing then
Response.Write("txt1 is nothing<br>" & vbCrLf)
Else
Response.Write("txt1 is something<br>" & vbCrLf)
End If
If isEmpty(Request.Form("txt1")) then
Response.Write("txt1 is empty<br>" & vbCrLf)
Else
Response.Write("txt1 is full<br>" & vbCrLf)
End If
If isNull(Request.Form("txt1")) then
Response.Write("txt1 is NULL<br>" & vbCrLf)
Else
Response.Write("txt1 is not NULL<br>" & vbCrLf)
End If
Response.Write("txt1 is of type: " & TypeName(Request.Form("txt1")) &
"<br /><br />" & vbCrLf)
Response.Write("Testing txt2<br />" & vbCrlf)
If Request.Form("txt2") is Nothing then
Response.Write("txt2 is nothing<br>" & vbCrLf)
Else
Response.Write("txt2 is something<br>" & vbCrLf)
End If
If isEmpty(Request.Form("txt2")) then
Response.Write("txt2 is empty<br>" & vbCrLf)
Else
Response.Write("txt2 is full<br>" & vbCrLf)
End If
If isNull(Request.Form("txt2")) then
Response.Write("txt2 is NULL<br>" & vbCrLf)
Else
Response.Write("txt2 is not NULL<br>" & vbCrLf)
End If
Response.Write("txt2 is of type: " & TypeName(Request.Form("txt2")) &
"<br>" & vbCrLf)
End If
%>
</body>
</html>
More information about the thelist
mailing list