/    Sign up×
Community /Pin to ProfileBookmark

JS – Form Validation – checkboxes

I’ve been given this task at work and am flying blind as a non-developer so any help much appreciated. I’ve got a form with

A set of 5 checkboxes (field = ‘risk’)
and
A separate checkbox (field = ‘no’)

The user can select from the top options, OR select No, but if they try and pick from the five, and also No – these are mutually exclusive and an error message is needed. I’ve tried a few things most recently:

function ValidateMe(f) {var maxchecked = 1;
var count = 0;
if(f.risk[0].checked == true) { count++; }
if(f.risk[1].checked == true) { count++; }
if(f.risk[2].checked == true) { count++; }
if(f.risk[3].checked == true) { count++; }
if(f.risk[4].checked == true) { count++; }
if((count >= maxchecked) && (f.no.checked == true))
{alert(‘Sorry, only a risk choice OR ‘No’ may be checked, but not both’);
return false;
}
return true;
}

and it doesn’t do anything – any ideas of why, or a better way to do this?

Thanks

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@crookedmonkeyauthorJun 25.2009 — this seems to work:

function ValidateMe(f) {

if (((f.risk[0].checked == true) || (f.risk[1].checked == true) || (f.risk[2].checked == true) || (f.risk[3].checked == true) || (f.risk[4].checked == true)) && (f.no.checked == true)) {

alert('Please select either a risk balancing option, or "No, I do not wish to accept", but NOT both');

return false;

}

return true;

I think!
×

Success!

Help @crookedmonkey 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 6.18,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...