Validate radio button list in javascript in asp.net
Here in this code snippet I will show how we can check if any of the radio button in the radio button list is selected or not before submitting the page in javascript in asp.net.
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('Please select a project!');
return false;
}
return true;
}
<asp:Button ID="btnSubmit" Text="Submit" runat="server" onclientclick=" return validateRadioButtonList('rdbProjectList');" OnClick="btnSubmit_Click" />
Here in the above code you can see when a user clicks on the submit button, client side code is executed first and checks if any of the item in the radio button list is selected. If any of the radio button is selected then page is submitted to server else user is prompt to select a radio button.
Hope this will help!!!!!!!!