Link to home
Start Free TrialLog in
Avatar of TonyReba
TonyRebaFlag for United States of America

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..
<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>

Open in new window

SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of TonyReba

ASKER

Client asked for a pop up message which I believe is gotta be java or ajax right?
In that case you can set ValidationSummary's property ShowMessageBox="true'
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
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.
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>

Open in new window

You should set the ErrorMessage like this:
ErrorMessage="Question 1 Required" Text="*"
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;
        }
this would be on the page init event? right?
You guys Rock!!