3

I am using c# for coding!

Below is my html for checkbox and radion button

  <input type="radio" style="float: left;" name="documents" id="Checkbox9" value="yes"
                        runat="server" />
                    <label style="width: 35px!important;" class="checkbox">
                        <%=GetResourceString("c_HSGStudent")%>
                    </label>
                    <input type="radio" style="float: left;" name="documents" id="Checkbox10" value="no"
                        runat="server" />
                    <label style="width: 25px!important;" class="checkbox">
                        <%=GetResourceString("c_HSGParent")%>
                    </label>
                    <input type="radio" style="float: left;" cheked name="documents" id="Radio1" value="yes"
                        runat="server" />
                    <label style="width: 35px!important;" class="checkbox">
                        <%=GetResourceString("c_HSGStudent")%>
                    </label>
                    <input type="radio" style="float: left;" name="documents" id="Radio2" value="no"
                        runat="server" />
                    <label style="width: 25px!important;" class="checkbox">
                        <%=GetResourceString("c_HSGParent")%>
                    </label>

You can see I have two checkboxes and two radio buttons, My problem is that on my submit button click I want to check whether user have checked at-least one checkbox or radio button. It will be good if we can have .NET solution like (customvalidator).

Please suggest!

Thanks

2 Answers 2

4

First, add a CustomValidator to your page...

<asp:CustomValidator runat="server" ID="CheckBoxRequired" EnableClientScript="true" 
    OnServerValidate="CheckBoxRequired_ServerValidate" 
    OnClientValidate="CheckBoxRequired_ClientValidate">*</asp:CustomValidator> 

You can then then validate them from a client side function with a simple jquery call...

<script type="text/javascript>

function CheckBoxRequired_ClientValidate(sender, e) 
{ 
    e.IsValid = $("input[name='documents']").is(':checked'); 
} 

</script>

code-behind for server side validation...

protected void CheckBoxRequired_ServerValidate(object sender, ServerValidateEventArgs e) 
{ 
    e.IsValid = Checkbox9.Checked || Checkbox10.Checked || Radio1.Checked || Radio2.Checked;
} 
Sign up to request clarification or add additional context in comments.

Comments

0

Create a custom validator and then check wether the controls meet the criteria in the servervalidate event handler.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.