Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

radio button validation.

Status
Not open for further replies.

Mayoor

Programmer
Jan 16, 2004
198
GB
I have a series of questions on a page each question has 3 radiobuttons. The number of questions on a page is dynamic and also the name of each group is dynamic as well depending on the id genrated from the DB.

I need some code which will loop through each group of radio sets and make sure that the user has answered at least one question.

Can this be done please?
 
While not wanting to discourage you from using a JS solution, if you have server-side technology available to you (which I'm assuming you do, since you mention DBs), you should do a server-side check regardless of the presence of a client-side check.

This will mean that people with JS disabled won't screw your back-end at all.

Hope this helps,
Dan



[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Yes the JS disabled has been a royal pain in... anywho.. in case you NEED javascript to do it, here it is:


ICOK=false; for (ic=0;ic<5;ic++)
{
if (document.form1.R1[ic].checked) ICOK=true;
}
if (!ICOK) {alertMsg += "\n- You didn't answer this section buddy!!";}


I would also recommend you use a server side checking.
 
I agree with Dan, there should always be a server-side process in place and JavaScript should merely be a first line of defense against data corruption. Anyway, here's my solution. I always use getElementsByName for radio buttons and checkboxes. That way it'll still work if there happens to be only one checkbox or radio button in a group.

Code:
var radioGroups = ["Group1","Group2","Group3"];
for(var i=0;i<radioGroups.length;i++){
  var answered = false;
  var radioGroup = document.getElementsByName(radioGroups[i]);
  for(var j=0;j<radioGroup.length;j++){
    if(radioGroup[j].checked){
      answered = true;
      break;
    }
  }
  if(!answered){
    alert('A question was left unanswered!');
    radioGroup[0].focus();
  }
}

Adam
 
Actually, let me change that so it only alerts them once. It could get annoying otherwise:
Code:
var radioGroups = ["Group1","Group2","Group3"];
for(var i=0;i<radioGroups.length;i++){
  var answered = false;
  var radioGroup = document.getElementsByName(radioGroups[i]);
  for(var j=0;j<radioGroup.length;j++){
    if(radioGroup[j].checked){
      answered = true;
      break;
    }
  }
  if(!answered){
    alert('A question was left unanswered!');
    radioGroup[0].focus();
    [b]break;[/b]
  }
}

Adam
 
Adam I dont think you understand. The Radio Groups are dynamically named based on the question ID in the database.

so for instance...

Code:
<INPUT TYPE='radio' NAME ='Question_<%=QuizQuestionsRS("QuestionID")%>' value='<%=QuizQuestionsRS("ChoiceID")%>'>


each radio button will have a subscript "Question_" and then the ID number.

Each group will have a different ID number.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top