/    Sign up×
Community /Pin to ProfileBookmark

Script Problem

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>

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@steelersfan88Jul 15.2004 — if both must validate:onsubmit="return (checkCheckBoxes(this) &amp;&amp; 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 &amp;&amp;
!form.CHECKBOX_2.checked &amp;&amp;
!form.CHECKBOX_3.checked &amp;&amp;
!form.CHECKBOX_4.checked &amp;&amp;
!form.CHECKBOX_5.checked &amp;&amp;
!form.CHECKBOX_6.checked &amp;&amp;
!form.CHECKBOX_7.checked &amp;&amp;
!form.CHECKBOX_8.checked &amp;&amp;
!form.CHECKBOX_9.checked)
Dr. Script
Copy linkTweet thisAlerts:
@GeneauthorJul 15.2004 — 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
Copy linkTweet thisAlerts:
@steelersfan88Jul 15.2004 — Yes, that would help.
×

Success!

Help @Gene spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.13,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...