[thelist] Checkboxes and the ASP Form collection

Ben Gustafson ben_gustafson at lionbridge.com
Fri Feb 8 10:36:00 CST 2002


I'm writing an ASP script to do dynamic server-side form validation. One
of the steps is to request the Form collection and iterate through it to
check the items. I'm having a problem with checkboxes and radio buttons,
though. I've found that if they are not checked, they do not get into
the Form collection. Text input elements do get into the collection if
they are empty, as long as a value attribute, empty or otherwise, is in
the Input tag. I could hard code the names of the checkbox form elements
into the validation script and flag them as invalid if they're not in
the collection, but that would defeat the purpose of making the
validation script dynamic. Does anyone have a way around this?

Here's the test code I'm using:

-----> form page:

	<html>
	<head>
		<title>greeting form</title>
	</head>

	<body>
	<form action="formGrab.asp" method="post">
	<INPUT TYPE="checkbox" NAME="chkHello" VALUE="hello">hello<BR>
	<INPUT TYPE="checkbox" NAME="chkGoodbye" VALUE="goodbye">goodbye<BR>
	<INPUT TYPE="checkbox" NAME="chkGoodbye" VALUE="'bye!">'bye!<BR>
	write stuff: <INPUT TYPE="text" NAME="frmStuff" VALUE=""><BR>
	<INPUT TYPE="submit">
	</form>
	</body>
	</html>

-----> ASP (formGrab.asp):

	<%@LANGUAGE="VBScript"%>
	<%
	Dim formItems
	Set formItems = Request.Form

	Response.Write "Number of form items: " & Request.Form.Count & "<BR>"

	Dim count
	count = 1
	For Each formName in formItems
		Response.Write "form name: " & formName & "<BR>"
		Response.Write "input: " & formItems(count) & "<BR>"
		count = count + 1
	Next
	%>

--Ben



More information about the thelist mailing list