'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.
Solution 1:[1]
Use Validators
<asp:RequiredFieldValidator
ID="ReqiredFieldValidator1"
runat="server"
ControlToValidate="RadioButtonList1"
ErrorMessage="Select your choice!"
>
</asp:RequiredFieldValidator>
Solution 2:[2]
You can use JQuery to validate this.
The statement $('#RadioButtonList1 input:checked').val() will give you the text of selected item otherwise null.
Solution 3:[3]
<form id="Form1" method="post" runat="server">
<asp:RadioButtonList id="RadioButtonList1" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Text="Excellent" Value="1" />
<asp:ListItem Text="Good" Value="2" />
<asp:ListItem Text="Average" Value="3" />
<asp:ListItem Text="Poor" Value="4" />
<asp:ListItem Text="Very Poor" Value="5" />
</asp:RadioButtonList>
<input type="button" onclick="onButtonClick();" value="Validate">
</form>
<script type="text/javascript">
<!--
function onButtonClick()
{
return validateRadioButtonList('<%= RadioButtonList1.ClientID %>');
}
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>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="return onButtonClick() ;" onclick="btnSubmit_Click" />
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jagz W |
| Solution 2 | IrfanRaza |
| Solution 3 | Nikhil D |
