Click to See Complete Forum and Search --> : radio validation not working


beachshooz
12-22-2003, 04:11 PM
I have a quiz that contains a form that uses radio buttons and values. There is a bit in the js code, on the 18th line or so, that should provide for an alert when some of the questions are not answered. It doesn't seem to be working and I don't know why. The alert is the only part of the code not working, everything else is fine.
<script language="Javascript">

function getVal( radiob ){
var val = 0;
for( i=0; i < radiob.length; i++ ){
if( radiob[i].checked ){
val = parseInt(radiob[i].value);
if(val == 'NaN') val = 0;
}
}
return val;
}

function doSum( formName ){
//add results from q1 through q2
var sum = 0;
var err = 0;
var msg = "Please answer all the questions.\n";
var temp;

temp = getVal( formName.q1 );
if( temp )
sum += temp;
else
err = 1;

temp = getVal( formName.q2 );
if( temp )
sum += temp;
else
err = 1;

temp = getVal( formName.q3 );
if( temp )
sum += temp;
else
err = 1;

temp = getVal( formName.q4 );
if( temp )
sum += temp;
else
err = 1;

temp = getVal( formName.q5 );
if( temp )
sum += temp;
else
err = 1;

//if( err ) alert( msg );
//else
window.location = "./results.shtml?" + sum;
}

</script>

The page is posted at: http://www.get-net-results.com/web/story_net_fit.html
Thanks for any suggestions.

woodknut
12-22-2003, 05:29 PM
SWAG:
I wonder if the variable 'err' is actually storing a numeric value (i.e. 1 or 0) that isn't being checked properly?

What happens if you change it to true/false?

I have seen scripts get "confused" when you use different formatting. I'd also suggest making the last test look just like the others; I know how "whitespace" is supposed to be seen by the parser but who knows?

if( err )
alert();
else
window.yada

W.

beachshooz
12-22-2003, 07:41 PM
It worked!!! The second idea. I took out the // in front of the if's and else's and it worked. I thank you, my client thanks you!

BS

woodknut
12-23-2003, 09:20 AM
Client?! CLIENT?!
We're doing this for money?
Cool!

:D :D

BTW Those // are how you create a comment and the text behind them are not seen by the parser. I though you put them there to identify the suspect code. Also notice the line:
"//add results from q1 through q2" ;)

beachshooz
12-23-2003, 10:35 AM
Well, it ain't much but a girl's gotta make a living. As penance, I will spend a couple hours helping solve some html problems in the General Forum...:)

Thanks for all of your help. This web site is a wonderful.