Validate radioButtonlist in asp.net using custom validator

<script type="text/javascript" language="javascript">
        function ClientValidate(source, args) {

            var MyRadioButton = document.getElementById("<%=rbl_Menu.ClientID %>");
            var options = MyRadioButton.getElementsByTagName("input");
            var Checkvalue = false;
            var check;
            for (i = 0; i < options.length; i++) {
                if (options[i].checked) {
                    Checkvalue = true;
                    check = options[i].value;
                }
            }
            if (!Checkvalue) {
                args.IsValid = false;
            }
            else {
                args.IsValid = true;
            }

        }
    </script>
<asp:RadioButtonList ID="rbl" runat="server" Width="30%"
RepeatDirection="Horizontal">
                                    <asp:ListItem Value="1">Data</asp:ListItem>
                                    <asp:ListItem Value="2">Data1</asp:ListItem>
                                </asp:RadioButtonList>
<asp:CustomValidator ID="cvType" ValidationGroup="imgsave" runat="server" ControlToValidate="rbl"
ClientValidationFunction="ClientValidate" Display="Dynamic" ValidateEmptyText="true" ErrorMessage="Please select Value">
</asp:CustomValidator>

Comments