SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    396
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Validating multiple radio buttons in an array?

    Hi,

    I'd like to basically check around 10 radio button selections on a page, and then if the user hasn't answered them, throw up an error message (or highlight the field etc) and not submit the form.

    This is the code I have at the moment:
    Code Javascript:
    function radio_button_checker() {
    	var radio_choice = false;
     
    	var array = ["document.survey.Question_1", "document.survey.Question_2", "document.survey.Question_3", "document.survey.Question_4", "document.survey.Question_5"]
     
    	for(var ind=0;(elem = array[ind]);ind++) {
    		for (counter = 0; counter < array[ind].length; counter++) {
    			if (array[ind].checked) {
    				radio_choice = true;
    			}
    			else {
    				// some code here that'll alert the user to the radio boxes they've missed out on
    			}
    		}
    	}
     
    	if (!radio_choice) {
    		alert('Not complete');
    		return (false);
    	}
    	return (true);
    }
    For some reason it alerts "Not complete" regardless of if I've selected any (or all) of the radio buttons.

    Thanks

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you also please show the HTML of the radio buttons?

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2006
    Location
    Australia
    Posts
    186
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have a look at the script I wrote here
    http://www.sitepoint.com/forums/showthread.php?t=542806

    It might do what you want, it is not complete for me yet.
    ========================
    Carn the Tiges!
    www.petalsandpatches.com

  4. #4
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    396
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    @ Pepe, the HTML for the buttons looks like this:

    Code HTML:
    <label class="question" for="Question_1"><span class="question_number">1)</span> Question here</label>
    <ul>
    	<li><input type="radio"  value="<?php echo $Level_1;?>" name="Question_1" id="<?php echo $Level_1;?>"> <span class="Normal"><label for="<?php echo $Level_1;?>">Strongly Agree</label></span></li>
    	<li><input type="radio"  value="<?php echo $Level_2;?>" name="Question_1" id="<?php echo $Level_2;?>"> <span class="Normal"><label for="<?php echo $Level_2;?>">Agree</label></span></li>
    	<li><input type="radio"  value="<?php echo $Level_3;?>" name="Question_1" id="<?php echo $Level_3;?>"> <span class="Normal"><label for="<?php echo $Level_3;?>">Disagree</label></span></li>
    	<li><input type="radio"  value="<?php echo $Level_4;?>" name="Question_1" id="<?php echo $Level_4;?>"> <span class="Normal"><label for="<?php echo $Level_4;?>">Strongly Disagree</label></span></li>
    	<li><input type="radio"  value="<?php echo $Level_5;?>" name="Question_1" id="<?php echo $Level_5;?>"> <span class="Normal"><label for="<?php echo $Level_5;?>">No or Insufficient Evidence</label></span></li>
    </ul>

    @ Tigers, that looks great, I'll get working on implementing it now, many thanks

  5. #5
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why not just have one of the radio buttons pre-selected?

  6. #6
    SitePoint Addict
    Join Date
    Jun 2007
    Posts
    396
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Good suggestion cheers, however it was a request from the start that they weren't pre-selected so we had to throw some PHP and Javascript validation in there It's working fine now though, so many thanks for the replies, I've just got it jumping to the question that hasn't been answered and a polite notice prompting the user to select an answer


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •