Click here to Skip to main content
15,881,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How i can apply javascript validation to check atleast one radiobutton list option is selected.

(Thanks in advance.)
Posted

hi,
I have validation radio button following way
C#
<script type="text/javascript" mode="hold" />function getName1()
{
    var rbCount = 0;
    var rbtable = document.getElementById('rblistInterests'); // Table Name => Your Radiobuttonlist ID
    var rbtr = rbtable.getElementsByTagName('tr');
    for(var i=0; i< rbtr.length;i++)
    {
        var rbtd = rbtr[i].getElementsByTagName('td');
        for(var j=0; j < rbtd.length; j++)
        {
            var rbinput = rbtd[j].getElementsByTagName('input');
            var rblabel= chktd[j].getElementsByTagName('label');
            for(k=0;k < rbinput.length;k++)
            {
                var rbopt = rbinput[k];
                if(rbopt.checked)
                {
                  return true; // At least one readio button selected.
                }
            }
        }
    }
    return false; // no one readiobutton selected.
}&lt;/script&gt;


I have radio button this way
XML
<asp:RadioButtonList runat="server" ID="rblistInterests" RepeatColumns="3" RepeatDirection="Vertical" RepeatLayout="Table">
        </asp:RadioButtonList>


Now take one button and take onclientclick event and call this function if atleast one readio button is selected then it is return true otherwise it is return false.
 
Share this answer
 
C#
<script type="text/javascript">
var wasOkPressed = false;
// Create an array of your checked controls
var controlArray = [ '<%= RadioButtonList1.ClientID %>'];
function validate() {
    wasOkPressed = false;
    // Loop through each control
    for(var i = 0; i < controlArray.length; i ++) {
        if (!wasOkPressed ) { // If ok was not pressed keep validating
            if (!validateRBL(controlArray[i])) {
                // If control fails to validate return false
                return false;
            }
        }
        wasOkPressed=true;
    }
    return true;
}
function validateRBL(RBLid)
{
 var listItemArray = document.getElementsByName(RBLid);
 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 )
     {
         var answer = confirm ("Please confirm selection...")
         if (answer)
         return true;
         else
         return false;
     }
 return true;
}
</script>



XML
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
      <asp:ListItem Value="1">Check 1</asp:ListItem>
      <asp:ListItem Value="2">Check 2</asp:ListItem>
  </asp:RadioButtonList>
  <input type="button" value="Validate Radio List" onclick="validate()" />


If i misunderstand your question, please feel free to correct me.
I hope the above information will be helpful. If you have more concerns, please let me know.
 
Share this answer
 
The whole point of a group of radio buttons is that one and only one is always selected: you don't need to validate this...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900