/    Sign up×
Community /Pin to ProfileBookmark

HTML form question

I have a form with two multi-selects and a series of checkboxes, used for searching a database. I’d like to limit the users to 5 selections overall – is there a way I can disable all three sets of form controls when a user reaches any possible combination 5 overall selections?

I’ve attached a sample of the html below as a text file – thanks!

[upl-file uuid=c200dc04-4a1a-4933-9508-10d986553145 size=4kB]searchpage.txt[/upl-file]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Michael2003Jul 24.2004 — Well if you first give the <form> a name (fmax in this case) you can then refer to its elements easily. Then you need to put a onchange event handler on each <input>. I'd suggest using a script to do this because it would take a long time to do it individually because there are so many of them. Then you need to have a function that runs through all the elements in a form, counts the number that is checked and disables them once they are all checked. So, in your page somewhere:

[code=php]<script type="text/javascript">
for(i=0; i<document.fmax.elements.length; i++){
document.fmax.elements[i].onchange = check;
}
function check(){
checks = 0;
for(i=0; i<document.fmax.elements.length; i++){
if(document.fmax.elements[i].checked == true){
checks++;
}
if(checks>3){
for(i=0; i<document.fmax.elements.length; i++){
document.fmax.elements[i].disabled = true;
}}}}
</script>[/code]

However, I would suggest not disabling the ones that are checked since then a user is locked into their first choice, even if they see a better option.
Copy linkTweet thisAlerts:
@ZeitauthorJul 24.2004 — Cool, thanks, I'll play around with that!
×

Success!

Help @Zeit 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.19,
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,
)...