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")
1
  • Is it classic ASP or ASP.NET? In other words, is it VBScript or VB.NET? Commented May 28, 2009 at 11:28

2 Answers 2

7

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
Sign up to request clarification or add additional context in comments.

1 Comment

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.
4

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
%>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.