Skip to content
Advertisement

How to give alert for user for select at least any one radio button?

<asp:RadioButtonList ID="rlist" runat="server"          
  style="z-index: 1; left: 194px; top: 69px; position: absolute;
  height: 21px; width: 110px" 
  RepeatDirection="Horizontal" Font-Bold="True" ForeColor="Black">
       <asp:ListItem Value="True">Yes</asp:ListItem>
       <asp:ListItem Value="False">No</asp:ListItem>
</asp:RadioButtonList>

This is my coding if user has not selected any one radio button means it will show alert in JavaScript.

Advertisement

Answer

Now it is tested please change your textbox Client Id and radio button ID. It fully works.

 <script type="text/javascript">
                function buttonclick()
                {
                    var txt1 = document.getElementById('<%=TextBox1.ClientID %>');
                    var txt2 = document.getElementById('<%=TextBox2.ClientID %>');
     

 var b = new Boolean(validateRadioButtonList('<%= rlist.ClientID %>'))
                     if (b == false)
                      {
                         return false;
                    
                      }
                    if (txt1.value == "")
                    {
                        alert("please enter text1");
                        return false;
                    }
                    if (txt2.value == "")
                    {
                        alert("please enter text2");
                        return false;
                    }
                    
                }
                function validateRadioButtonList(radioButtonListId)
                {
                    var listItemArray = document.getElementsByName(radioButtonListId);
                    var isItemChecked = false;
        
                    for (var i = 0; i < listItemArray.length; i++)
                    {
                        var listItem = listItemArray[i];
        
                        if (listItem.checked)
                        {
                            //alert(listItem.value);
                            isItemChecked = true;
                        }
                    }
        
                    if (isItemChecked == false)
                    {
                        alert('Nothing is checked!');
        
                        return false;
                    }
        
                    return true;
                }
            
            </script>
        </head>
        <body>
            <form id="form1" runat="server">
            <div>
                <asp:RadioButtonList ID="rlist" runat="server" style="z-index: 1; left: 194px; top: 69px; position: absolute; height: 21px; width: 110px" RepeatDirection="Horizontal" Font-Bold="True" ForeColor="Black">
 <asp:ListItem Value="True">Yes</asp:ListItem> 
<asp:ListItem Value="False">No</asp:ListItem> 
</asp:RadioButtonList> 
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                <asp:Button ID="Button1" runat="server" OnClientClick="return buttonclick();" Text="Button" />
            </div>
            </form>
</body>
Advertisement