Solved

Validation of Radio buttons

Posted on 2001-07-18
3
164 Views
Last Modified: 2006-11-17
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].

0
Comment
Question by:Jeff_Keeler
3 Comments
 
LVL 4

Accepted Solution

by:
ckosloski earned 200 total points
ID: 6296078
    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
 
LVL 1

Expert Comment

by:poluru
ID: 6298125
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
 

Author Comment

by:Jeff_Keeler
ID: 6298358
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now