Click to See Complete Forum and Search --> : Script Problem


Gene
07-14-2004, 09:32 PM
Hello all. Below is the routines I am using to validate checkboxes in a form.

there are 9 checkboxes in all. All can be checked.

If number 9 checkbox is checked (Other) then the focus is set to a textfield for an explanation.

Here is the OnSubmit code...

onsubmit="return checkCheckBoxes(this),checkOther(this);"


the problem is this. Only the last validation command works, which ever is last works.

How can I make them both work?

Thanks



<script type="text/javascript" language="JavaScript">
<!--
function checkCheckBoxes(form) {
if (
form.CHECKBOX_1.checked == false &&
form.CHECKBOX_2.checked == false &&
form.CHECKBOX_3.checked == false &&
form.CHECKBOX_4.checked == false &&
form.CHECKBOX_5.checked == false &&
form.CHECKBOX_6.checked == false &&
form.CHECKBOX_7.checked == false &&
form.CHECKBOX_8.checked == false &&
form.CHECKBOX_9.checked == false)
{
alert ('You must check a box!');
return false;
} else {
return true;
}
}

function checkOther(form) {
if (form.CHECKBOX_9.checked) {
if (form.ExplainOther.value.length > 0) {
return true;
}
else {
alert ("Please explain your Other selection.");
form.ExplainOther.focus();
return false;
}
}
else {
return false;
}
}
// End -->
</script>

steelersfan88
07-14-2004, 10:36 PM
if both must validate:onsubmit="return (checkCheckBoxes(this) && checkOther(this));"or if only 1 has to validate:onsubmit="return (checkCheckBoxes(this) || checkOther(this));"by the way, you could use:if (
!form.CHECKBOX_1.checked &&
!form.CHECKBOX_2.checked &&
!form.CHECKBOX_3.checked &&
!form.CHECKBOX_4.checked &&
!form.CHECKBOX_5.checked &&
!form.CHECKBOX_6.checked &&
!form.CHECKBOX_7.checked &&
!form.CHECKBOX_8.checked &&
!form.CHECKBOX_9.checked)Dr. Script

Gene
07-15-2004, 06:49 AM
I tried the top version and what happens is this. If a user tries to submit without entering anything, when they go back and check any box, it automatically submits the form, even if they checked the Other box which should make them enter something in the textfield for Other. But it does not, just sends the form on its own.

Both functions must validate in order for it to work properly, but it is driving me crazy.

Would it help for you to view the page in action to see what we are trying to achieve?

Gene

steelersfan88
07-15-2004, 10:18 AM
Yes, that would help.