Avatar of saturation
saturation
 asked on

ASP.NET CustomValidator on checkbox

I'm trying to validate a checkbox with the CustomValidator, but it doesn't seem to be firing.  Does it have to do with the fact that I have some regular validator tags on other fields that are firing when the submit button is clicked?   They all work just fine--but this one doesn't.  There's no message, and no error message, either.  

.ASPX PAGE
<asp:CheckBox ID="chkConsent" runat="server" />
<asp:CustomValidator id="cust_CheckBoxPrint" runat="server" ErrorMessage="Required!" OnServerValidate="ValidateCheckbox">* required</asp:CustomValidator>

CODEBEHIND PAGE
    Sub ValidateCheckbox(ByVal source As Object, ByVal args As ServerValidateEventArgs)
        args.IsValid = (chkConsent.Checked = True)
    End Sub
ASP.NETVisual Basic.NET

Avatar of undefined
Last Comment
Nasir Razzaq

8/22/2022 - Mon
Stephan

Are you having other validation controls on the page and are they validating on the client-side?

If so, you could add a piece of javascript in order to validate the checkbox on client-side.
rajeeshmca

Hi,

Try with the following. I had a try with ur code in c# and it worked.

Sub ValidateCheckbox(ByVal source As Object, ByVal args As ServerValidateEventArgs)
        If chkConsent.Checked = True Then
              args.IsValid = True
        Else
              args.IsValid = False
       End IF
    End Sub

<form id="form1" runat="server">
   
    <asp:CheckBox ID="chkConsent" runat="server" />
    <asp:CustomValidator ID="cust_CheckBoxPrint" runat="server" ErrorMessage="Required!"
        OnServerValidate="ValidateCheckbox">* required</asp:CustomValidator>
    <asp:Button ID="Button1" Text="Submit" runat="server"/>
    </form>
rajeeshmca

Also the ControlToValidate property is also missing...
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
saturation

ASKER
So, I've noticed one more thing...The checkbox unchecked does prevent the page from being submitted, but there is no message that comes up if it's not checked.   Any ideas?
ASKER CERTIFIED SOLUTION
Stephan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Stephan

Make sure when you disable client validation that you need to add if(Page.IsValid) { //validated }
On your event where you want to process the posted data
Alpesh Patel

write code in form submit event (saving data to DB).
 if (!cust_CheckBoxPrint.IsValid)
                    {
                       
                    }
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Nasir Razzaq

Does it show the error message if you use

<asp:CheckBox ID="chkConsent" runat="server" />
<asp:CustomValidator id="cust_CheckBoxPrint" runat="server" ErrorMessage="Required!" OnServerValidate="ValidateCheckbox" Text="* required" ErrorMessage="* Required" />

?