Validation of Radio buttons

Can someone please help me with a loop to do the validation checking that this function does.  I was able do to this for index[0] of all the named radiobuttons but for some reason i cannot figure a way to make it loop through and check the radio buttons for index[1] - [15].


here is my code example

function Enter_onclick()
{
     var arrrb1 = document.forms[0].elements['T1.1'];
     var objrb1 = arrrb1[0];
     var arrrb2 = document.forms[0].elements['T1.2'];
     var objrb2 = arrrb2[0];
     var arrrb3 = document.forms[0].elements['T1.3'];
     var objrb3 = arrrb3[0];
     var arrrb4 = document.forms[0].elements['T1.4'];
     var objrb4 = arrrb4[0];
     var arrrb5 = document.forms[0].elements['T1.5'];
     var objrb5 = arrrb5[0];
     var myerror = "You have selected more than one Ranking"    
     
         
         
          if (objrb1.checked == true && objrb2.checked == true || objrb1.checked == true && objrb3.checked == true || objrb1.checked == true && objrb4.checked == true || objrb1.checked == true && objrb5.checked == true)
          {
               alert(myerror);
          }
          if (objrb2.checked == true && objrb3.checked == true || objrb2.checked == true && objrb4.checked == true || objrb2.checked == true && objrb5.checked == true)
          {
               alert(myerror);

          }
          if (objrb3.checked == true && objrb4.checked == true || objrb3.checked == true && objrb5.checked == true)
          {
               alert(myerror);
          }
          if (objrb4.checked == true && objrb5.checked == true)
          {
               alert(myerror);
          }
}
</Script>


Imagine for a sec that you have 5 rows of radio buttons each row has a
different name and there are 16 radio buttons for each row:

        1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
 T1.1   o o o o o o o o o o  o  o  o  o  o  o
 T1.2   o o o o o o o o o o  o  o  o  o  o  o
 T1.3   o o o o o o o o o o  o  o  o  o  o  o
 T1.4   o o o o o o o o o o  o  o  o  o  o  o
 T1.5   o o o o o o o o o o  o  o  o  o  o  o

 the o represents the button

now what am i trying to do?  I do not want a user to be able to select the
same value 1-16 in the different rows T1.1 - T1.5.  meaning

 This would be invalid

        1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
 T1.1   x o o o o o o o o o  o  o  o  o  o  o
 T1.2   x o o o o o o o o o  o  o  o  o  o  o
 T1.3   o o x o o o o o o o  o  o  o  o  o  o
 T1.4   o o x o o o o o o o  o  o  o  o  o  o
 T1.5   o o o x o o o o o o  o  o  o  o  o  o

x represents a checked radio button

however this would be valid


        1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
 T1.1   x o o o o o o o o o  o  o  o  o  o  o
 T1.2   o x o o o o o o o o  o  o  o  o  o  o
 T1.3   o o x o o o o o o o  o  o  o  o  o  o
 T1.4   o o o o o o x o o o  o  o  o  o  o  o
 T1.5   o o o x o o o o o o  o  o  o  o  o  o

Now I wrote some code mentioned above but it only works for the first
value or index[0].  However I do not understand how to create a loop to
check for index[1], index[2],... index[15].

Jeff_KeelerAsked:
Who is Participating?
 
ckosloskiConnect With a Mentor Commented:
    function Enter_onclick()
     {
          var arrrb1 = document.forms[0].elements['T1.1'];
          var arrrb2 = document.forms[0].elements['T1.2'];
          var arrrb3 = document.forms[0].elements['T1.3'];
          var arrrb4 = document.forms[0].elements['T1.4'];
          var arrrb5 = document.forms[0].elements['T1.5'];
          var myerror = "You have selected more than one Ranking";

          for(x=0;x<15;x++)
          {
               var objrb1 = arrrb1[x];
               var objrb2 = arrrb2[x];
               var objrb3 = arrrb3[x];
               var objrb4 = arrrb4[x];
               var objrb5 = arrrb5[x];

               if((objrb1.checked == true && objrb2.checked == true) || (objrb1.checked == true && objrb3.checked == true) || (objrb1.checked == true && objrb4.checked )== true || (objrb1.checked == true && objrb5.checked == true))
               {
                    alert(myerror);
               }
               if((objrb2.checked == true && objrb3.checked == true) || (objrb2.checked == true && objrb4.checked == true) || (objrb2.checked == true && objrb5.checked == true))
               {
                    alert(myerror);
               }
               if((objrb3.checked == true && objrb4.checked == true) || (objrb3.checked == true && objrb5.checked == true))
               {
                    alert(myerror);
               }
               if(objrb4.checked == true && objrb5.checked == true)
               {
                    alert(myerror);
               }
      }
     }
0
 
poluruCommented:
just a small change
try the below code

function Enter_onclick()
    {
         var arrrb1 = document.forms[0].elements['T1.1'];
         var arrrb2 = document.forms[0].elements['T1.2'];
         var arrrb3 = document.forms[0].elements['T1.3'];
         var arrrb4 = document.forms[0].elements['T1.4'];
         var arrrb5 = document.forms[0].elements['T1.5'];
         var myerror = "You have selected more than one Ranking";

         for(x=0;x<15;x++)
         {
              var objrb1 = arrrb1[x];
              var objrb2 = arrrb2[x];
              var objrb3 = arrrb3[x];
              var objrb4 = arrrb4[x];
              var objrb5 = arrrb5[x];

              var i=0;
             
          if(objrb1.checked)i++;
          if(objrb2.checked)i++;
          if(objrb3.checked)i++;
          if(objrb4.checked)i++;
          if(objrb5.checked)i++;
                if(i>1) alert(myerror);
             
          }
    }
0
 
Jeff_KeelerAuthor Commented:
In order to prevent a fight both codes worked extremely well.  And I thank both you ckosloski and poluru for you time and effort for helping me through this loop.  Now once I understand how this works I will be at a position do to it myself without help (but that is for me to figure out).  Anyway like I said both codes do work however, I am using the code ckosloski provided since this allows me to break the loop after the user has clicked the ok button on the alert error.  The code provided by Poluru did shorten the code and was nice however if somone had selected for example value 1 for T1.1 and T1.2 and value 2 for T1.3 and T1.4 his code would produce 2 alert boxes.  Since I am not that proficient with the language yet I did not know how to change his code to produce the breaks so that only one error message came up.

Again Thank you and have a wonderful GOD filled Day!!  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.