Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: Re: Requiring a selection in all sets of radio buttons


Message #1 by "Stephen D. Oliver" <steve@o...> on Sat, 20 Apr 2002 23:02:08
> 
> I have multiple forms with varying numbers of sets of radio buttons for 
an 
> online survey.  I would like to use a script that will loop through all 
> the sets of radio buttons and make sure they all have a response.
> 
> (The first element of each form is always a hidden field, the rest are 
> always sets of radio buttons, so that's why I'm starting at element[1])
> 
> I have come up with this code, but it always returns true.  Any 
assistance 
> will be much appreciated.
> 
> function validate(form) { // form is passed as 'document.forms.myForm'
>    for (var i=1; i<form.elements.length; i++) {
>       var radioSet = form.elements[i];
>       var radioSetChecked = false;
>       for (var j=0; j<radioSet.length; j++) {
>          if (radioSet[j].checked)
>          radioSetChecked = true;
>       }
>       if (radioSetChecked = false) {
>          alert('Please rate all objectives');
>          return false;
>       }
>    }
>    return true;

Hmm. I'm struggling with the same issue.  I don't want to have to specify 
any control by name; I just want the code to loop through the existing 
controls and check to make sure they've been checked or have input.
Problem is, the radio buttons are always in groups with the same names.  
For example, "Yes" + "No".  I don't want to know if each and every one is 
checked necessarily, I just need to know that of a given group, at least 
one is checked.
So far, this is the code that I've come up with (sorry about the white 
space mish mash--it didn't copy and paste well):

<script language="Javascript">
        
        function checkForm(form) {
            var strElements = "";
            var iElements = form.length;
            
            for (var i = 0; i <= (iElements) - 1; ++i) {
                
                if (form.elements[i].type == "radio" && form.elements
[i].checked == false) {
                    strElements = strElements + form.elements[i].name 
+ "\r";
                    }
              
                if (form.elements[i].type == "text" && form.elements
[i].value == "") {
                    strElements = strElements + form.elements[i].name 
+ "\r";
                    }    
            }
            alert("The number of elements on this page is: " + iElements 
+ "\r" + "The following controls exist on this form: " + "\r\r" + 
strElements);
        }
                      
        </script>

This checks to see what type it is, radio or text and, depending upon the 
type, appends a second qualification that is pertinent to the type, 
e.g., "checked" for radio, and "" for text. Then it pops up an alert that 
tells the user which 
My problem is, again, I need to check whether or not one of a GROUP of 
radio buttons has been checked, NOT EACH and every radio button.

  Return to Index