I'm having a problem which I thought would be a very simple thing but is
> > turning out to be a difficult challenge. I have a web form with several input
> > fields and I have a single checkbox, not a checkbox list. Before the form can
> > be submitted I want to require that the checkbox be checked. Sounds simple
> > but non of the validation controls seem to work correct for this, or maybe
> > I'm just not using them correctly. I have done several searches but I can't
> > seem to find any examples of this type of thing being done. Which is very
> > surprising because this sort of checkbox is a very common thing. i.e. "I
> > agree to the terms....." Can anyone point me to an example? I'm using ASP.NET 2.0
Mark Rae [MVP] - 19 Sep 2007 20:43 GMT
> Can anyone point me to an example?
<script type="text/javascript">
function validateForm()
{
if (!document.getElementById('<%=MyCheckBox.ClientID%>').checked)
{
alert ('You must agree to the terms');
document.getElementById('<%=MyCheckBox.ClientID%>').focus();
return false;
}
}
</script>
<form id="form1" runat="server">
<asp:CheckBox ID="MyCheckBox" runat="server" />
<asp:Button ID="MyButton" Text="Submit" OnClick="MyButton_Click"
OnClientClick="return validateForm();" />
</form>

Signature
Mark Rae
ASP.NET MVP
http://www.markrae.net
bruce barker - 19 Sep 2007 20:57 GMT
pretty trival:
<script type="text/javascript">
function ackCheck(s,a)
{
a.IsValid = document.getElementById('<%=chkAck.ClientID%>').checked;
return a.IsValid;
}
</script>
<asp:CustomValidator runat=server
ControlToValidate="chkAck"
ErrorMessage="Please Acknowledge"
ClientValidationFunction="ackCheck"
Display="Dynamic"
/>
<asp:checkbox runat=server id="chkAck"
Text="Acknowledge" />
-- bruce (sqlwork.com)
> I'm having a problem which I thought would be a very simple thing but is
>>> turning out to be a difficult challenge. I have a web form with several input
[quoted text clipped - 5 lines]
>>> surprising because this sort of checkbox is a very common thing. i.e. "I
>>> agree to the terms....." Can anyone point me to an example? I'm using ASP.NET 2.0