0

I need to show an asterix to the correspondent checkbox when the user try to send the form without check the checkbox... I found something like this but is not working, like the normal others <asp:RequiredFieldValidator fields:

<asp:CustomValidator  ID="CheckBoxValidator" runat="server" onservervalidate="CheckBoxRequired_ServerValidate" Display="Dynamic" CssClass="error-text" ErrorMessage='*' ValidationGroup="save"> * </asp:CustomValidator>

asp:CheckBox ID="YourCheckBox" runat="server" text=" I accept your terms and conditions bla bla bla" />

And in the code behind I have:

protected void CheckBoxRequired_ServerValidate(object sender, ServerValidateEventArgs e)
{
  e.IsValid = YourCheckBox.Checked;
  labelMessage.Text = "Your reservation has been processed."; 
}
neophyte
  • 6,540
  • 2
  • 28
  • 43
Cris
  • 1
  • 1
  • 5
  • possible duplicate of [How do I make a checkbox required on an ASP.NET form?](http://stackoverflow.com/questions/1228112/how-do-i-make-a-checkbox-required-on-an-asp-net-form) – Mate Feb 25 '15 at 10:35
  • try adding ControlToValidate="YourCheckboxId" in your custom validator. – Vivek Mishra Feb 25 '15 at 10:39

1 Answers1

0

Look at this blog post. this is what you need

http://www.aspsnippets.com/Articles/ASP.Net-CheckBox-Required-Validation-using-Custom-Validator-and-JavaScript.aspx

[Edit]

Here I've applied some modification to code from link

<script type = "text/javascript">
        function ValidateCheckBox(sender, args) {            
            if (document.getElementById("CheckBox1").checked == true) {
                args.IsValid = true;
            } else {
                args.IsValid = false;
            }
        }
    </script> 

<asp:CheckBox ID="CheckBox1" ClientIDMode="Static" runat="server" />
    <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="CustomValidator" EnableClientScript="true" ValidationGroup="ggg" ClientValidationFunction="ValidateCheckBox"></asp:CustomValidator>
    <asp:Button ID="Button1" runat="server" Text="Submit" ValidationGroup="ggg" /> 

[Edit 2] In order to validation on server only, remove ClientValidationFunction and ValidationGroup and do this

protected void Button1_Click(object sender, EventArgs e)
        {
            if (!CheckBox1.Checked)
            {
                CustomValidator1.IsValid = false;
            }
        }
Michael Samteladze
  • 1,310
  • 15
  • 38
  • no it doesnt work, someone can write something working? there must be some piece missing in the examples i find on internet – Cris Feb 25 '15 at 10:54
  • now the asterix compare correctly, only, when i submit without any check the page refresh and send the email to address. Because i have other fields who have a server validation in the form. In general it would be better to know how to have all server or all client validation but i dont know how to do the server validation with aspnet for the checkbox. Please help. – Cris Feb 25 '15 at 11:33
  • In practice now compare asterix for all the fields including checkbox, (thanks to this piece of javascript) and immediately after the page is refreshed, in practice without checking anything. There is probably some no synchronization betweeen server validators and this javascript validation – Cris Feb 25 '15 at 11:35
  • Solved thanks, but I don't understand why clearly, I tried also similar commands before and didn't work, it seems It's due to some change in the parameters of 'Custom Validator'... – Cris Feb 27 '15 at 09:51