Solved

Validation of Radio buttons

Posted on 2001-07-18
3
163 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
Comment Utility
    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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

771 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

10 Experts available now in Live!

Get 1:1 Help Now