Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Apr 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Multiple Radio Button Validation

    Hi all,

    This is my first post here so be kind!

    I am writing a small questionairre in which I wish to validate that all questions have been answered. The problem I run into is when I want to validate the radio buttons on the page.

    I have a set of questions from 1 to 30. Each radio button set is named Q1, Q2, Q3 and so on...

    I have written some code to check each question using a for loop (which loops 30 times), within that I have another loop that check to see if the current radio button set the code is looping through is check or not. If it isn't it adds on a message to my output message.

    I am pretty sure that my theory is correct, it's seems to be my error/syntax is not quite right.

    Here is the full code - hope I have explained well enough!

    Code:
    <%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Survey</title>
    <style type="text/css">
    <!--
    .main {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 71%; border: 1px solid #CCCCCC; }
    body {
    	background-color: #FFFFFF;
    }
    -->
    </style>
    <script language="JavaScript" type="text/JavaScript">
    <!--
    function CheckForm()
    	{
    	var returnval=false; 
    	var msg=""; 
    	var counter;
    	var checked = false;
    	
    	if(document.frmSurvey.txtFullName.value=="") 
    	   msg+="Please enter your name.\n";
    	if(document.frmSurvey.txtForm.value=="") 
    	   msg+="Please enter your form.\n";
    	   
    	// Loop from zero to the one minus the number of radio button selections
    	for (var x = 1; x <= 30; x++) 
    	{
    		for (counter = 0; counter < document.frmSurvey.Q+x.length; counter++)
    		{
    			if (!document.frmSurvey.Q+x+[counter].checked){
    				msg+="You have not answered question "+[x]+".\n";
    			}
    		}
    	}
    	   
    	if(msg=="") 
    	   { 
    	   returnval=true; 
    	   return returnval; 
    	   } 
    	else 
    	   { 
    	   alert(msg); 
    	   return returnval; 
    	   } 
    }
    //-->
    </script>
    </head>
    
    <body>
    <form name="frmSurvey" id="frmSurvey" action="submit.asp" method="post" onsubmit="return CheckForm()">
    <table class="main" width="425" border="0" cellspacing="0" cellpadding="5">
      <tr>
        <td width="85"><strong>Full Name</strong></td>
        <td width="5">:</td>
        <td width="305"><input name="txtFullName" type="text" id="txtFullName" size="40" maxlength="100" /></td>
      </tr>
      <tr>
        <td><strong>Form</strong></td>
        <td>:</td>
        <td><input name="txtForm" type="text" id="txtForm" size="5" maxlength="4" /></td>
      </tr>
    </table>
    <br />
    <table width="747" border="0" cellpadding="5" cellspacing="1" class="main">
      <tr bgcolor="#F5F5F5">
        <td width="328">I felt that I was part of a school community</td>
        <td width="147"><input name="Q1" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q1" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q1" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q1" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr>
        <td width="328">Subject staff knew my strengths and weaknesses</td>
        <td width="147"><input name="Q2" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q2" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q2" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q2" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr bgcolor="#F5F5F5">
        <td width="328">Subject staff wanted me to succeed</td>
        <td width="147"><input name="Q3" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q3" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q3" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q3" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr>
        <td width="328">Subject staff helped me achieve my potential</td>
        <td width="147"><input name="Q4" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q4" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q4" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q4" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr bgcolor="#F5F5F5">
        <td width="328">My tutor knew my strengths and weaknesses</td>
        <td width="147"><input name="Q5" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q5" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q5" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q5" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr>
        <td width="328">My tutor wanted me to succeed</td>
        <td width="147"><input name="Q6" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q6" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q6" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q6" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr bgcolor="#F5F5F5">
        <td width="328">My tutor helped me achieve my potential</td>
        <td width="147"><input name="Q7" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q7" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q7" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q7" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr>
        <td width="328">I knew how well I was doing in lessons</td>
        <td width="147"><input name="Q8" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q8" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q8" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q8" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr bgcolor="#F5F5F5">
        <td width="328">I knew what sort of grades I should be achieving at GCSE</td>
        <td width="147"><input name="Q9" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q9" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q9" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q9" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr>
        <td width="328">I knew what I needed to do to improve my performance</td>
        <td width="147"><input name="Q10" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q10" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q10" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q10" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr bgcolor="#F5F5F5">
        <td width="328">I was helped to develop study skills</td>
        <td width="147"><input name="Q11" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q11" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q11" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q11" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr>
        <td width="328">I received guidance on how to revise</td>
        <td width="147"><input name="Q12" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q12" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q12" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q12" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr bgcolor="#F5F5F5">
        <td width="328">I was given guidance on what to revise</td>
        <td width="147"><input name="Q13" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q13" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q13" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q13" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr>
        <td width="328">I found lessons easy</td>
        <td width="147"><input name="Q14" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q14" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q14" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q14" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr bgcolor="#F5F5F5">
        <td width="328">I found lessons difficult</td>
        <td width="147"><input name="Q15" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q15" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q15" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q15" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr>
        <td width="328">Support was available when I found lessons difficult</td>
        <td width="147"><input name="Q16" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q16" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q16" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q16" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr bgcolor="#F5F5F5">
        <td width="328">The resources available for learning were good</td>
        <td width="147"><input name="Q17" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q17" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q17" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q17" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr>
        <td width="328">I believe that homework activities are important</td>
        <td width="147"><input name="Q18" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q18" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q18" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q18" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr bgcolor="#F5F5F5">
        <td width="328">I know what is expected of me for homework</td>
        <td width="147"><input name="Q19" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q19" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q19" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q19" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr>
        <td width="328">I completed homework to the best of my ability</td>
        <td width="147"><input name="Q20" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q20" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q20" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q20" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr bgcolor="#F5F5F5">
        <td width="328">I revised for exams to the best of my ability</td>
        <td width="147"><input name="Q21" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q21" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q21" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q21" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr>
        <td width="328">Discipline at South Wilts is fair</td>
        <td width="147"><input name="Q22" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q22" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q22" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q22" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr bgcolor="#F5F5F5">
        <td width="328">Students are treated equally</td>
        <td width="147"><input name="Q23" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q23" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q23" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q23" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr>
        <td width="328">There are good opportunities to gain rewards</td>
        <td width="147"><input name="Q24" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q24" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q24" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q24" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr bgcolor="#F5F5F5">
        <td width="328">There are good opportunities to succeed outside the classroom</td>
        <td width="147"><input name="Q25" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q25" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q25" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q25" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr>
        <td width="328">Staff encourage students to take part in extra-curricular activities</td>
        <td width="147"><input name="Q26" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q26" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q26" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q26" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr bgcolor="#F5F5F5">
        <td width="328">Staff encourage students to take on responsibilities in the school</td>
        <td width="147"><input name="Q27" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q27" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q27" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q27" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr>
        <td width="328">I enjoyed my experiences at South Wilts</td>
        <td width="147"><input name="Q28" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q28" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q28" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q28" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr bgcolor="#F5F5F5">
        <td width="328">The information I received on opportunities after 16 was helpful</td>
        <td width="147"><input name="Q29" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q29" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q29" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q29" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr>
        <td width="328">I was given individual help in planning for my future</td>
        <td width="147"><input name="Q30" type="radio" value="1" />
        Disagree Strongly</td>
        <td width="87"><input name="Q30" type="radio" value="2" />
        Disagree</td>
        <td width="72"><input name="Q30" type="radio" value="3" />
        Agree</td>
        <td width="122"><input name="Q30" type="radio" value="4" />
        Agree Strongly</td>
      </tr>
      <tr>
        <td colspan="5"><div align="center">
          <br />
          <input name="cmdSubmit" type="submit" id="cmdSubmit" value="Submit Answers" />
        </div></td>
      </tr>
    </table>
    </form>
    </body>
    </html>
    TIA
    Last edited by gadago; 04-25-2006 at 02:48 PM.

  • #2
    Regular Coder
    Join Date
    Jan 2006
    Location
    Preston, Lancashire, England
    Posts
    285
    Thanks
    0
    Thanked 0 Times in 0 Posts
    something like this should do i think
    Code:
    var question3= false
    for (counter = 0; counter < mainform.q3.length; counter++)
    {			
    if (mainform.q3[counter].checked)
    {
    question3= true; 
    break;
    }
    }		
    if(question3== false)
    {
    errormessage  = errormessage + "Please answer question3\n";
    }
    of course that will only do question three. you would either need to create one of these for ever set of radio buttons or add to the code to loop through every set of them.
    Last edited by MRMAN; 04-25-2006 at 03:03 PM.

  • #3
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In JavaScript dot notation can't handle string contanation or the use of variables -- you need to use array notation for that.

    Try this.
    Code:
    for (counter = 0; counter < document.frmSurvey.elements['Q'+x].length; counter++)
    {
    	if (!document.frmSurvey.elements['Q'+x][counter].checked){
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #4
    New to the CF scene
    Join Date
    Apr 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your replies.

    Kravvitz method seems to have solved my problem.

    The only problem now is that it seems my loop is a little messed up. It displays the error message 4 times for each radio group that has not been ticked or 3 times if one of the group has been selected. The problem obviously lies within the inner loop but I can't figure out how to sort it.

    Code:
    for (var x = 1; x <= 30; x++) 
    	{
    		for (counter = 0; counter < document.frmSurvey.elements['Q'+x].length; counter++)
    		{
    			if (!document.frmSurvey.elements['Q'+x][counter].checked){
    				msg+="You have not answered question "+[x]+".\n";
    			}
    		}
    	}
    Any ideas?

  • #5
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,511
    Thanks
    3
    Thanked 504 Times in 491 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    <!--
    var RadNames=['R1','R2','R3'];
    // Radio buttons with the same name form an array
    // This is one simple method of checking the objjects in each group array
    function Validate(frm){
     var zxcmess=[];
     for (var zxc0=0;zxc0<RadNames.length;zxc0++){
      var radobjs=frm[RadNames[zxc0]];
      var val=false;
      for (var zxc1=0;zxc1<radobjs.length;zxc1++){
       if (radobjs[zxc1].checked){
        val=true;
       }
      }
      if (!val){
       zxcmess.push('Please Check One from group '+(radobjs[0].title||radobjs[0].name));
      }
     }
     if (zxcmess.length>0){
      alert(zxcmess.join('\n'));
      return false;
     }
     return true;
    }
    
    //-->
    </script></head>
    
    <body>
    <form name="fred" >
    <input title="Question 1" type="radio" name="R1">
    <input type="radio" name="R1">
    <input type="radio" name="R1">
    <input type="radio" name="R1">
    <br>
    <input title="Question 2" type="radio" name="R2">
    <input type="radio" name="R2">
    <input type="radio" name="R2">
    <input type="radio" name="R2">
    <br>
    <input title="Question 3" type="radio" name="R3">
    <input type="radio" name="R3">
    <input type="radio" name="R3">
    <input type="radio" name="R3">
    <br>
    <input type="button" name="" value="Validate" onclick=" Validate(this.form);" >
    
    </form>
    </body>
    
    </html>
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #6
    New to the CF scene
    Join Date
    Apr 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    Managed to get it all working with this code if any else is interested.

    Code:
    for (var x = 1; x <= 30; x++) 
    	{
    		checked=false;
    		for (counter = 0; counter < document.frmSurvey.elements['Q'+x].length; counter++)
    		{
    			if (document.frmSurvey.elements['Q'+x][counter].checked)
    			{
    				checked=true;
    			}
    		}
    		if (!checked)
    		{
    			msg+="You have not answered question "+[x]+".\n";
    		}
    	}
    Thanks for all your help!


  •  

    Posting Permissions

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