TonyReba
asked on
Validate multiple radiobuttonlist validation with error message each group
I have this codes to validate a set of radiobutton list , list items controls in my questionarie., question is how can I extend it to validate all questions e.g.
radiobuttonlist2, radiobuttonlist3,.......
and display which question was not not answered. ,,,,
thanks and look forward for assistance..
radiobuttonlist2, radiobuttonlist3,.......
and display which question was not not answered. ,,,,
thanks and look forward for assistance..
<script type = "text/javascript">
function Validate() {
var RB1 = document.getElementById("<%=RadioButtonList1.ClientID%>");
var radio = RB1.getElementsByTagName("input");
var isChecked = false;
for (var i = 0; i < radio.length; i++) {
if (radio[i].checked) {
isChecked = true;
break;
}
}
if (!isChecked) {
alert("Please select an option in each question");
}
return isChecked;
}
</script>
Control:
<asp:RadioButtonList ID="RadioButtonList1" repeatdirection="Vertical" runat="server">
<asp:ListItem Value="Yes"></asp:ListItem>
<asp:ListItem Value="No"></asp:ListItem>
</asp:RadioButtonList>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In that case you can set ValidationSummary's property ShowMessageBox="true'
ASKER
Cool! Both you guys Rock.. guru sami , I was able to show the popup message but how do I make it inidicate which question Needs to be answered , is there a property..?
screen.png
screen.png
The ErrorMessage property of RequiredFieldValidator control is the one that shows up in the ValidationSummary and the Text property of RFV is the one that shows up next to the RBL.
So adjust them accordingly.
So adjust them accordingly.
ASKER
I am trying but dont get it to work as wished,
<asp:ValidationSummary ID="ValidationSummary1" runat="server"
ShowMessageBox="True" ShowSummary="False" HeaderText="You must enter a value in the following fields:"/>
.
<asp:RadioButtonList ID="RadioButtonList1" repeatdirection="Vertical" runat="server">
<asp:ListItem Value="Yes"></asp:ListItem>
<asp:ListItem Value="No"></asp:ListItem>
</asp:RadioButtonList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="RadioButtonList1" ErrorMessage="* Required" Text="Question 1"></asp:RequiredFieldValidator>
You should set the ErrorMessage like this:
ErrorMessage="Question 1 Required" Text="*"
ErrorMessage="Question 1 Required" Text="*"
ASKER
Thanks I get your point now and I got it working , do you happend to know how do I clear all radio buttons when I reload the page??
Loop through each RBL item and set it's selection to false...
foreach (ListItem item in RadioButtonList1.Items)
{
item.Selected = false;
}
foreach (ListItem item in RadioButtonList1.Items)
{
item.Selected = false;
}
ASKER
this would be on the page init event? right?
ASKER
You guys Rock!!
ASKER