Reposted this as I am still awaiting a response to my question
Initial Post
--------------------------------------------------------------------------------
Here is my problem. Basically I am designing a quiz that validates each input as per what the user chooses and will not let them proceed without answering all questions correctly. At the moment I can do this for one question using name, however when I have multiple questions (I tried changing my script to validate by id) it only validates the first one, whichever is clicked first. At the moment I am validating onclick for ease of testing however the final quiz will validate onsubmit. Here is my code it would be great if anyone can tell my how I can get it to validate all questions (there will be more)
<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
function checkAnswers()
{
var retval = false;
for(i=0;i<document.multichoice.question.length;i++)
{
if(document.multichoice.question[i].value == "yes" && document.multichoice.question[i].checked == true)
{
retval = true;
break;
}
}
if(retval == false)
alert("That is the wrong answer");
return retval;
}
I thought about that way but thought there must be an easier better code solution, say for example I had 300 questions then that is alot of repetitive code, so was trying to work out a way way to put it in a loop
Thanks
Response 2
#4 04-17-2009, 03:53 AM
WolfShade
Registered User Join Date: Dec 2002
Location: CHICAGO, baby!
Posts: 283
Not the greatest idea, but you could nest the FOR loops as long as all the radio button names keep the same designation format ("qu plus number").
Code:
for(i=1; i<[number of questions]; i++) { //Since the names start with 1, use 1-based index instead of 0-based as it would normally be for this kind of array
for (j=1; j<document.multichoice.qu[j].length; j++) {
.. blah blah blah ..
}
}^_^
My second response to responses
#5 04-17-2009, 10:43 AM
JscrptBeginner
Registered User Join Date: Apr 2009
Posts: 7
See post #6 of http://www.webdeveloper.com/forum/sh....php?t=2071066
for an alternate solution. See how the validation sections for radio buttons and select boxes was handled as well as single and multiple checkbox clicks.
Should be modifiable for a lot of questions, but 300 on one quiz would sure be a bummer to take, especially on-line!
Thanks JMRKER, unfortunately that link doesn't seem to work and I can't seem to find that thread by the number. However I actually reposted this thread again here http://www.webdeveloper.com/forum/sh...478#post999478 as I didnt seem to be getting any responses.
As you can see in this other thread I received a response from ZeroKilled that seems to work well. Let me know if this is different from your solution as I would like to learn if there is a better way.
my mom is javascript, dad is javascripter, granpa is javascriptor, and my little sister is javasRidiculous. my nature language is javascript, then come spanish and english -- me
Yep found it now, thanks to both of you ZeroKilled and JMRKR both solutions work great in different ways, now I just need to decide which way is better on submit or on click. Happy days, thanks again
Yep found it now, thanks to both of you ZeroKilled and JMRKER both solutions work great in different ways, now I just need to decide which way is better on submit or on click. Happy days, thanks again
Bookmarks