Asp form - checkbox default value

Hope this is the correct category for an asp code question.

I have the following code

	FOR i = 1 TO 20 : strCampo(i,0) = "" : strCampo(i,1) = "no" : 	NEXT

	'strPrivacy = "no"
	'strCampo(1,0) =  "Username:"              :  strCampo(1,1) = "si"
	'strCampo(2,0) =  "Password:"              :  strCampo(2,1) = "si"
	'strCampo(3,0) =  "Conferma password:"     :  strCampo(3,1) = "si"
	strCampo(4,0) =  "Email:"                 :  strCampo(4,1) = "si"
	strCampo(5,0) =  "Conferma email:"        :  strCampo(5,1) = "si"
	strCampo(6,0) =  "Nome e Cognome:"         :  strCampo(6,1) = "si"
	'strCampo(7,0) =  "Cognome:"               :  strCampo(7,1) = "si"
	'strCampo(8,0) =  "Ditta:"                 :  strCampo(8,1) = "si"
	'strCampo(9,0) =  "Indirizzo1:"             :  strCampo(9,1) = "no"
	'strCampo(10,0) = "Indirizzo2:"            : strCampo(10,1) = "no"
	'strCampo(11,0) = "CAP:"                   : strCampo(11,1) = "no"
	'strCampo(12,0) = "Provincia:"             : strCampo(12,1) = "no"
	strCampo(13,0) = "Città:"                 : strCampo(13,1) = "si"
	'strCampo(14,0) = "Ambiente:"              : strCampo(14,1) = "si"
	'strCampo(15,0) = "Larghezza(metri):"      : strCampo(15,1) = "si"
	'strCampo(16,0) = "Lunghezza(metri):"      : strCampo(16,1) = "si"
	'strCampo(17,0) = "Telefono:"              : strCampo(17,1) = "no"
	'strCampo(18,0) = "Fax:"                   : strCampo(18,1) = "si"
	'strCampo(19,0) = "Sito web:"              : strCampo(19,1) = "si"
	strCampo(20,0) = "Messaggio:"             : strCampo(20,1) = "si"

	FUNCTION IsGiust(strStringa, strPattern)
		Dim objRegExp
		Set objRegExp = New RegExp
		objRegExp.Pattern = strPattern
		objRegExp.IgnoreCase = True
		IsGiust = objRegExp.Test(strStringa)
	END FUNCTION

	IF Request.QueryString("step") = 2 THEN

After that there are some controls (error checks) for the fields like this

		FOR i = 1 to 20
			IF strCampo(i,1) = "si" THEN
				IF LEN(Request("T"&i)) < 1 THEN
					strErrore(i) = "Campo obbligatorio"
					strErrore(0) = "errore"
				END IF
			END IF
		NEXT
	
		'##### [T1] USER #####################################################
		IF strCampo(1,0) <> "" AND LEN(Request("T1")) <> 0 THEN
			IF LEN(Request("T1")) < 5 THEN
				strErrore(1) = "Inserisci almeno 5 caratteri"
				strErrore(0) = "errore"
			END IF
		END IF

then I have the following form

<form method="POST" action="<%=Request.ServerVariables ("URL")%>?step=2#Modulo">
	<div class="text-center">
		<p class="text-center">
		(i campi con asterisco <span class="fz-required">*</span> sono obbligatori)
		</p>
	</div>

<!-- ######### bootstrap ######### -->

<div class="fz-form-box">

<% FOR i = 1 TO 19 %>
	<% IF strCampo(i,0) <> "" THEN %>
	
	
	<div class="form-group fz-name clearfix">
		<label class="col-md-3" for="name"><%=strCampo(i,0)%> </label>
		<div class="col-md-1 fz-required">
			<% IF strCampo(i,1) = "si" THEN Response.Write ("<b>*</b>") %>
		</div>
		<div class="col-md-5">
			<input name="T<%=i%>" tabindex="<%=i%>" value="<%=Request("T"&i)%>"
				<% IF InStr( 1, strCampo(i,0), "password", 1) THEN %>
					<%= " type='password'" %>
				<% ELSE %>
					<%= " type='text'" %>
				<% END IF %>
				<% IF strErrore(i) <> "" THEN %>
					<%= " class='form-control has-error'" %>
				<% ELSE %>
					<%= " class='form-control'" %>
				<% END IF %>
			>
		</div>
		<div class="col-md-3 fz-required">
			<%=strErrore(i)%>
		</div>
	</div>

	<% END IF %>

<% NEXT %>
<% IF strCampo(20,0) <> "" THEN %>


	<div class="form-group fz-name clearfix">
		<label class="col-md-3" for="name"><%=strCampo(20,0)%> 
		</label>
		<div class="col-md-1 fz-required">
			<% IF strCampo(20,1) = "si" THEN Response.Write ("<b>*</b>") %>
		</div>
		<div class="col-md-5">
			<textarea id="textarea" name="T20" tabindex="20"
				<% IF strErrore(20) <> "" THEN %>
					<%= " class='form-control has-error'" %>
				<% ELSE %>
					<%= " class='form-control'" %>
				<% END IF %>
				><%=Request("T20")%>
			</textarea>
		</div>
		<div class="col-md-3 fz-required">
			<%=strErrore(i)%>
		</div>
	</div>

<% END IF %>


	<% IF strPrivacy = "no" THEN %>

	<div class="text-center">
		<input class="fz-button" type="submit" value="Invia" name="B1" id="invia">
	</div>

	<% ELSE %>

	<div class="form-group fz-name clearfix">
		<label class="col-md-2" for="name">Privacy:</label>
		<div class="col-md-1">
			<input class="form-control" type="checkbox" checked name="C1" value="ON" onclick="
				if (document.getElementById('invia').disabled == true)
					{document.getElementById('invia').disabled = false}
				else {document.getElementById('invia').disabled = true}
				">
		</div>
		<div class="col-md-9">
			Acconsento al trattamento dei dati personali ai sensi e per gli effetti del D. Lgs. 196/2003
		</div>
	</div>

	<div class="form-group clearfix">
		<input class="fz-button" type="submit" value="Invia" name="B1" id="invia">
	</div>

	<% END IF %>
</div>
</form>

The above works ok, only problem is it outputs the privacy checkbox already checked.
I’d like to have it unchecked, and make it required for the user to check before submitting.

Thank you.

Ok I am going to assume that the checkbox you speak of is the “C1” checkbox. Notice in the HTML there you have “checked”

<input class=“form-control” type=“checkbox” checked name=“C1” value=“ON”

Because that is there, the checkbox will show as checked. Removing it will cause it to be unchecked at the start. Of course if you want to set it checked/unchecked based on some variable, you will have to build in the condition to output "checked’ or blank depending on the value.

:slight_smile:

Thank you.
By removing checked it does display unchecked, but when checking it the send (invia) button is disabled, and can’t submit.

Also could you please kindly in case address the reuired part of it.

Simple solution… disable the button first. Make the button by default disabled. So that when you click the checkbox, it will then enable it using the logic you already have there.

<input class=“fz-button” type=“submit” value=“Invia” name=“B1” disabled id=“invia”

As for requiring it to be checked before submitting, well, two processes usually are done. One client side using JavaScript, but also server side (ASP) to make sure that the user is not trying to get around the system.

So for the JavaScript part, on submit of the form, check that the C1 checkbox field is checked. I think you probably know how to do that. Then, on the server side, in your script when the form is submitted and you are looking at the request, make sure that C1 element submitted is also checked… aka (C1.Checked == true)

:slight_smile:

1 Like

Thank you.

Unforunatelly it doesn’t seem to work.
By disabling the B1 as per your suggestion nothing changes, as C1 stays checked
By disabling the B1 and removing checked from C1 it is the behaviour described in previous post:
checkbox is unchecked, but when checking it the send button is disabled.

I think you probably know how to do that.

Not really, specially asp related, if I knew what I was doing I wouldn’t have asked :blush:

That said I think the original author (I don’t know who) made it so that it is required, but the inverse way, meaning if you unckeck the checkbox the form can’t be sent, as submit button turns disabled.
So the form is actually behaving as it should, but usually it is better to have the user check the box instead of an already checked one.

Could the solution be in this snip, in order to invert the process?

onclick="
				if (document.getElementById('invia').disabled == true)
					{document.getElementById('invia').disabled = false}
				else {document.getElementById('invia').disabled = true}
				"

Huh? Disable the button for invia… then when you click the checkbox the snippet will read “Is invia disabled” yes, so it will enable it. I mean just look at the logic there. I also tested it out here for you…

https://jsfiddle.net/5hy4Lgmk/

Very simple idea. The button is disabled by default, your checkbox is unchecked by default and when you check, it runs your code to enable the button. Simple.

1 Like

Thank you for your patience, and of course the solution.

My bad, previous post consequence of my mistakes :man_facepalming:.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.