Link to home
Start Free TrialLog in
Avatar of sammySeltzer
sammySeltzerFlag for United States of America

asked on

Can someone please show me a sample jquery or regular javascript that can validate a RadioButtonList in Repeater control?

Greetings again,

I have this RadioButtonList control with a Yes or option in Repeater control.

                 Purchased:<asp:radiobuttonlist ID="rblType" runat="server" RepeatDirection="Horizontal" TextAlign="Right" style="display:inline;">
                 <asp:ListItem Text="New" />
                 <asp:ListItem Text="Used" />
                </asp:RadioButtonList>

Open in new window


All I am trying to do is to ensure that user chooses either Yes or No before continuing.

I have done a lot RadioButtonList validations but have not found anything that works with Repeater control.

Any ideas experts?
Avatar of HainKurt
HainKurt
Flag of Canada image

how that piece of code is rendered on browser? post that code...

try this... does it fire every time you click radio?

Purchased:
<asp:radiobuttonlist ID="rblType" runat="server" RepeatDirection="Horizontal" TextAlign="Right" style="display:inline;">
                 <asp:ListItem Text="New" onClick="rbTypeClick(this)" />
                 <asp:ListItem Text="Used" onClick="rbTypeClick(this)" />
</asp:RadioButtonList>

<script>
rbClicked = false;
function rbTypeClick(this){
  rbClicked = true; alert(rbClicked);
}
</script>

Open in new window

Avatar of sammySeltzer

ASKER

Hi HainKurt,

Thanks for your response.

It gives me this error:

Warning            The use of a keyword for an identifier is invalid      

function rbTypeClick(this)

Open in new window


Only thing I have tried so far is regular RadioButtonList validation.

However, I have managed to use the following for disabling and enabling a textbox. I just could not modify it to use for validation that says:

Please choose Yes or No.

 <script type="text/javascript">
     $(document).on("click", "input:radio[name$='rblType']", function(){
         var selectedItems = $("input:radio[name$='rblType'][value='No']:checked");
         $("#textbox1").prop("disabled", selectedItems.length === 0);
     });
    </script>

Open in new window


Only thing I was able to find online so far.

Would you be able to modify to give me what I am looking for?

Repeater is one control I have had more problems using dropdownlist and radiobuttonlist.
sorry, use

function rbTypeClick(rb)

Open in new window


whats the purpose of those buttons?
what will happen if user clicks or dont click?
Good question.

That's exactly what I am trying to do.

If user does not click on either Yes or No, we want to stop the user from continuing by giving a message to the user that either Yes or No must be selected in other to proceed.
If user does not click on either Yes or No, we want to stop the user from continuing by giving a message to the user that either Yes or No must be selected in other to proceed.

so, you dont need to do anything here...
when you continue, check the radio button value. and if it is not checked, stop there...
meantime, use regular radio buttons, or asp radiobuttons for this purpose... if you want to validate this on client...

do you have any test page?
Hi,

Not sure of what type of test page you need?

But here is one I hope:

      <asp:Repeater ID="Repeater2" runat="server" OnItemDataBound="repeater_ItemDataBound">
        <ItemTemplate>
         <table style="width:100%;border: 1px solid black;">
             <tr>
              <td style="width:50%;border-collapse: collapse;border: 1px solid black;">
                 Purchased:<asp:radiobuttonlist ID="rblType" runat="server" RepeatDirection="Horizontal" TextAlign="Right" style="display:inline;">
                 <asp:ListItem Text="Yes" />
                 <asp:ListItem Text="No" />
                </asp:RadioButtonList>
             </td>
           </tr>
        </table>
      </ItemTemplate>
     </asp:Repeater>

Open in new window


User tries to submit without checking either the Yes button or No button, then an error should be raised.

Something like:

Please choose Yes or No

I was trying to add it to this jquery:

    <script type ="text/javascript" >                 
        $(document).ready(function () {               
            $("#form1").validate({                  
                rules: {   
                     <%=rblType.UniqueID %>: {                            
                         required:true,                            
                     },  
                },   
                messages: {   
                    //This section we need to place our custom validation message for each control.   
                   <%=rblType.UniqueID %>: {   
                       required: "Please choose either Yes or No."   
                   }, 
                },                  
            });   
        });          
    </script> 

Open in new window


It failed with error that it did not recognize rblType.

This is the harsh reality of Repeater. I am using it here out of need.

Thanks
ok, just use regular radio buttons, give same name and you should be ok... and use those names in your script...

html
Purchased:
<input type=radio name=rblType value='N'> New
<input type=radio name=rblType value='U'> Used

Open in new window


css
input[type="radio"] {
  margin-left: 30px;
}

Open in new window


js
$("#form1").validate({
  rules: {
    rblType: {
      required: true,
    },
  },
  messages: {
    //This section we need to place our custom validation message for each control.   
    rblType: {
      required: "Please choose either Yes or No."
    },
  },
});

Open in new window


I used
https://jqueryvalidation.org/

demo
https://jsfiddle.net/ey1nu1wh/

* in demo, needs to adjust the message position, I leave it to you, since I am not sure what plugin you use...
Well, that worked.

Thanks a bunch for your efforts.

Problem though is that using this regular html radio buttons without the runat="server"

<input type=radio name=rblType value='N'> New
<input type=radio name=rblType value='U'> Used

Open in new window


means I can't insert the values into the database; at least I am not aware of that is possible.
yes you can...
on submit, just use Request("rblType")
it will give you N or U
ASKER CERTIFIED 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
Thank you very much sir.