views:

1579

answers:

2

I have a html page with a form that has some check boxes. I need, using VbScript ASP, to make sure that one checkbox is checked. How do I do that? Here's the checkbox itself:

Dim terms
terms = Request.Form("terms")
+3  A: 

If the checkbox is checked, it's value will be sent in the form data, otherwise no item for the field is send in the form data. If you don't specify a value for the checkox, the default value "on" is used.

So, to determine if the checkbox is checked, compare against the value:

If terms = "on" Then
   ...
End If
Guffa
That, and (terms not = "") works nicely.
Raithlin
Comparing against an empty string is a bit shaky, as the value actually isn't an empty string if the checkbox is not checked. The value is Empty (not assigned) in that case.
Guffa
This did the trick. Thanks!
A: 

The best way is to explicitly give your checkbox a value:

<input type="checkbox" name="terms" value="Yes">

Then you can check whether the field contains the value you set:

<%
Dim terms
terms = Request.Form("terms")
If terms = "Yes" Then
    //...your code here
End If
%>

If you don't know what value the checkbox has (or if you have no control over its value), you can check for an empty string. Yes, theoretically speaking the form returns the special value 'Empty', not a zero-length string, for an unchecked (or nonexistent) field; but in practice, the Request.Form converts Empty to a blank string anyway.

<input type="checkbox" name="terms">
<%
Dim terms
terms = Request.Form("terms")
If terms <> "" Then
   //...checkbox was checked
End If
%>
Martha