PDA

Click to See Complete Forum and Search --> : javascript time - tZ where you at.


Neballer
11-08-2007, 11:35 PM
I'm not going to pretend that I know that much at all about javascript.
With that being said, I present you with a function that loops through radio buttons from a quiz and checks to see if they're..um..checked. This works, but since the alert is in the loop, I get a pop up for every radio button that's not checked. I only want one.

I tried something along the lines of adding to a message to a variable every time it got into the !checked, but the alert didn't work for me outside the loop. Soooooooodas anyone have any ideas for me.


function validate_form ( ) {
valid = true;
for (var x = 1; x <= 5; x++) {
checked=false;
for (counter = 0; counter < document.pgOneForm.elements['q'+x].length; counter++) {
if (document.pgOneForm.elements['q'+x][counter].checked) {
checked=true;
}
}
if (!checked) {
alert ( "You have not answered question "+[x]+".\n" );
valid = false;
}
}
return valid;
}

hewligan
11-08-2007, 11:52 PM
Shouldn't this work?

function validate_form ( ) {
valid = true;
var validMessage = "";
for (var x = 1; x <= 5; x++) {
checked=false;
for (counter = 0; counter < document.pgOneForm.elements['q'+x].length; counter++) {
if (document.pgOneForm.elements['q'+x][counter].checked) {
checked=true;
}
}
if (!checked) {
validMessage = validMessage + "You have not answered question "+[x]+".\n";
valid = false;
}
}
if( ! valid) {
alert(validMessage);
}
return valid;
}

Neballer
11-09-2007, 12:03 AM
should, and does. Thanks Hewligan.
tZ you've be usurped :D

hewligan
11-09-2007, 12:07 AM
should, and does. Thanks Hewligan.
tZ you've be usurped :D

I wish ;)

tZ still kicks my arse when it comes to javascript.