Quick javascript question

pushVTEC

Senior member
Aug 30, 2003
265
0
0
Ok what is wrong with the way i'm checking the radio buttons? It ran properly before I changed it, and now it doesn't work it always submits the form. Website is at: http://www.users.muohio.edu/pa...jscript/javascript.php

<html>
<head>
<title>Javascript</title>
<script type ="text/javascript">
<!-- //This line is here in case a browser is not js compatible...
function validate() {
var msg = '';

if(document.theForm.name.value==''){
msg += 'Enter a name please\n';
}
if(document.theForm.color.value==''){
msg += 'Enter a color please\n';
}
if(document.theForm.animal.value==''){
msg += 'Enter animal please\n';
}
if(document.theForm.teacher.value==''){
msg += 'Enter a teacher name please\n';
}
if(!radio_checker(document.theForm.sex)){
msg += 'Select a gender please\n';
}
if(!radio_checker(document.theForm.food)){
msg += 'Pick your favorite food\n';
}
if(document.theForm.money.value==''){
msg += 'Enter a sum of money please\n';
}
if(msg != ''){
window.alert(msg);
return false;
}
else{
document.theForm.submit();
}

}

function radio_checker(f){
var selected = false;
for (counter = 0; counter < f.length; counter++){
if (f[counter].checked)
selected = true;
}
if(!selected){
return false;
}
else{
return true;
}
}
}

//This line is here in case a browser is not js compatible... -->
</script>
<link rel="stylesheet" type="text/css" href="deco.css">
</head>
<body>
<div id="welcome">
Welcome to the "verbulator" online. Enter the information below and click submit to then get your information "verbulated". </br>
</div>
<div id="formQ">
<form action="results.php" method="post" name="theForm" onsubmit="validate(); return false;">
Your name? <input type="text" name="name" /> <br/>
Favorite color? <input type="text" name="color" /> <br/>
Favorite animal <input type="text" name="animal" /> <br/>
A miami professor <input type="text" name="teacher" /> <br/>
Are you male? Yes<input type="radio" name="sex" value="male" /> No <input type="radio" name="sex" value="female" /><br/>
Favorite food? <br/>Pizza<input type ="radio" name="food" value="pizza" /> <br/>
Steak<input type="radio" name="food" value="steak"/> <br/>
Fish<input type="radio" name="food" value="fish" /><br/>
Salad <input type="radio" name="food" value="salad"/> <br/>
Enter a sum of money <input type="text" name="money" /><br/>
<input name="submit" type="submit"><input name="reset" type="reset">

</form>
</div>
</body>

</html>
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0