ASP.NET Checkbox & the asp:CustomValidator

windows

Guest
Been running into this issue and can't seem to get around it. I've done various forum and web searches but I still haven't found a way to get around it.

Basically I have a check box for 'Terms & Conditions' on my signup page. I also have several required textbox fields which successfully use the <asp:RequiredVieldValidator>. Obviously, if a customer clicks the 'submit' button...if the 'Terms & Conditions' checkbox is not checked then I want an error thrown.

I am using the following...

<asp:checkbox id="Checkbox2" tabIndex="39" CssClass="text_std_bold" runat="server"></asp:checkbox>
<div class="text_std_bold" style="DISPLAY:inline; CURSOR:pointer">
<span width="555px">
<a onclick='javascript:window.open("/registration/TermsConditions.htm",
"Terms_and_Conditions","top=175,left=425,
toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no,
resizable=no, copyhistory=no, width=400, height=600")'>
<u>Terms and Conditions</u>
</a>
</span>
</div>
<FONT color="red" size="2"> *</FONT><br>
<asp:CustomValidator Runat=server Display=Dynamic
ID="CustValidator" ClientValidationFunction="validateCheckBox2"
ErrorMessage="Please agree to the Terms and Conditions"></asp:CustomValidator>


Sub validateCheckbox2(ByVal source As System.Object, ByVal s As ServerValidateEventArgs)
If (Me.Checkbox2.Checked = True) Then
s.IsValid = True
Else
s.IsValid = False
End If
End Sub

And the error I get is: Error: Object Expected.

And this is from the asp:CustomValidation line...so I'm not really sure how to get this working. I tried following this link:

<!-- m --><a class="postlink" href="http://www.dotnet247.com/247reference/msgs/40/204990.aspx">http://www.dotnet247.com/247reference/m ... 04990.aspx</a><!-- m -->

But to no avail...I'm using VS.NET with VB.NET.

I need it to integrate with the rest of my validation checking...so was hoping to not use any javascript solution. But if anyone has any thoughts I would great, greatly appreciate it. Thanks alot!It does not appear that you have properly assigned your validator to your checkbox field.

Go into the properties, make sure that's set properly and then try it out.you need to concatenate your script in a string then call Page.RegisterClientScript(strScript)

that will allow your datagrid and such to call it.Originally posted by putts
It does not appear that you have properly assigned your validator to your checkbox field.

Go into the properties, make sure that's set properly and then try it out.

I am unable to use the 'ControlToValidate="Checkbox2"' assignment in the asp:CustomValidator. That was the first thing I tried, but I was greeted with the error saying it was not allowed. Perhaps you cannot validate asp:checkboxes like that?Originally posted by afterburn
you need to concatenate your script in a string then call Page.RegisterClientScript(strScript)

that will allow your datagrid and such to call it.

Gotcha, I thought I was missing some way to register it. I'll give this a go.

Thanks again guys for the great help. :)Well I'm still having issues here. I put the following line in the Page_Load() of my ascx.vb file

Page.RegisterClientScriptBlock("ValidateCheckBox2", "<SCRIPT LANGUAGE=Javascript>function validateCheckBox2(oSrc, args){args.IsValid = document.all[Checkbox2].checked;}</SCRIPT>")


Is this the right place to put it in order to register it? and is this in the right format?

When I get to the page that uses the validation, every seems to work fine (you can't continue if you don't check the checkbox) but even if you do check the box there warning in the status bar of IE (lower left corner) that says there's an error "And object is expected" and I'm not sure what it's referring to.

I have the following sub in the ascx.vb file as well

Private Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
If (Checkbox2.Checked) Then
args.IsValid = True
Else
args.IsValid = False
End If

End Sub

This is pretty much just like the example on the first link I posted...just changed to VB instead of C#.

Finally, the code for the actual checkbox itself

<TD vAlign="top" colSpan="4">
<asp:checkbox id="Checkbox2" tabIndex="39" CssClass="text_std_bold" runat="server"></asp:checkbox>
<div class="text_std_bold" style="DISPLAY:inline; CURSOR:pointer">
<span width="555px">
<a onclick='javascript:window.open("/registration/TermsConditions.htm","Terms_and_Conditions","top=175,left=425,toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=400, height=600")'>
<u>Terms and Conditions</u>
</a>
</span>
</div>
<FONT color="red" size="2"> *</FONT><br>
<asp:CustomValidator id="CustomValidator1" Font-Names="Verdana" Font-Size="XX-Small" Display="Dynamic" ClientValidationFunction="validateCheckBox2" runat="server" ErrorMessage="Please Agree to the Terms and Conditions"></asp:CustomValidator>
</TD>

Sorry to keep bugging on this, just am confussed on to what the error is coming from? Any thoughts? :(document.all[Checkbox2].checked


is invalid javascript as "CheckBox2" is not a string or an integer.Originally posted by afterburn
document.all[Checkbox2].checked


is invalid javascript as "CheckBox2" is not a string or an integer.

That is indeed exactly it afterburn, thanks again. :)

Checkbox2 is the ID for the checkbox I am trying to validate. I was working from this example (<!-- m --><a class="postlink" href="http://www.dotnet247.com/247reference/msgs/40/204990.aspx">http://www.dotnet247.com/247reference/m ... 04990.aspx</a><!-- m -->) and tried to replicate it as close as I could.
That example uses 'Checkbox2.ID' so perhaps I should adjust it to 'Checkbox2.ID.ToString' to make it valid JS? I appreciate the great help as always. :DThat should correct your javascript, I will take a deeper look later about the rest.
 
Back
Top