Search code examples
javascriptvalidationradio-button

validate radio button list in javascript


    <asp:RadioButtonList ID="rblradio" runat="server">

        <asp:ListItem Value="M" Text="Male"></asp:ListItem>

        <asp:ListItem Value="F" Text="Female"></asp:ListItem>

    </asp:RadioButtonList>

How to validate this radio button list in javascript i am doing like this event if i

selected any option it saying not selected

function ValidateAccessLevel() {

        var AccessLevelIndexValue = document.getElementById('<%=rblradio.ClientID%>');

         if (AccessLevelIndexValue.checked)
         {
            return false;

            }

        else
        {
            return true;
        }
    }

</script>

Solution

  • Since you're already using an ASP RadioButtonList to render your radio buttons, I'd suggest to stick to the ASP framework and use an ASP RequiredFieldValidator to make the radio buttons required.

    <asp:RequiredFieldValidator 
        id="radioValidator 
        runat="server" 
        ControlToValidate="rblradio"
        Display="Dynamic">
        *
    </asp:RequiredFieldValidator>
    

    How to use requiredfieldvalidator for radio button in aspnet