PDA

View Full Version : Problems getting radio buttons to validate



shehaal
01-28-2008, 04:37 AM
Hi there,

I have a submission form on my site with a number of textboxes, two radio buttons, and a checkbox. When the user submits the form, it calls a Javascript function to go through and make sure everything that is required, has been entered/selected.

I am having troubles getting the radio buttons to validate. For some reason, when it hits that point in the function, it seems to do what it's supposed to (ie., check to make sure one of two two buttons is selected), but then it submits the form without going through the rest of the function.


function contactValidator(formName) {
if (formName.firstname.value == "") {
alert("Please enter your first name.");
formName.firstname.focus();
return (false);
}
if (formName.surname.value == "") {
alert("Please enter your surname.");
formName.surname.focus();
return (false);
}
if (formName.senderemail.value == "") {
alert("Please enter your email address.");
formName.senderemail.focus();
return (false);
}
// Check to make sure a radio button has been selected
var setflag = false;
for (var i = 0; i < formName.freewebmail.length; i ++) {
if (formName.freewebmail[i].checked) {
setflag = true;
}
}
if (!setflag) {
alert("Please indicate whether you would like a free webmail account.");
formName.freewebmail.focus();
return (false);
}
if (formName.agreement.checked == false) {
alert("You must check the checkbox to acknowledge your acceptance of the Agreement and submit the form.");
formName.agreement.focus();
return (false);
}

return (true);
}

Any ideas on why it would submit the form without finishing going through the rest of the function?

Thanks for your help.

Cheers,
Stuart.