Rayna,

Assuming that all of your checkboxes have the same name, but different
values, this script will work.

function countChecked(TheForm)
{
        var cChecked = 0;
        for( var i=0;i<TheForm.Item.length;i++ )
        {
                if( TheForm.Item[i].checked == true )
                        cChecked++;
        }
        if( cChecked > 5 )
        {
                alert('You must choose 5 or less items.');
                return false;
        }
        return true;
}

<FORM NAME="ChoiceForm" ACTION="#Wherever#" METHOD="POST" ONSUBMIT="return
countChecked(this);">
<INPUT TYPE="checkbox" NAME="Item" VALUE="#ItemID#">
... end form

If however you have checkboxes of different names then you would want to
change the function to this (assuming that each item checkbox name had the
same name fragment for instance "Choice1,Choice2,Choice3,...").

function countChecked(TheForm)
{
        var cChecked = 0;
        for( var i=0;i<TheForm.elements.length;i++ )
        {
                if( TheForm.elements[i].type = 'checkbox' &&
TheForm.elements[i].name.search(/^Choice/) != -1 &&
TheForm.elements[i].checked == true )
                        cChecked++;
        }
        if( cChecked > 5 )
        {
                alert('You must choose 5 or less items.');
                return false;
        }
        return true;
}

HTH as I'm writing without a page to test the code. It should work
though.... <crosses fingers>

-Nate


-----Original Message-----
From: Rayna Evans [mailto:[EMAIL PROTECTED]]
Sent: Thursday, August 30, 2001 4:01 PM
To: CF-Talk
Subject: javascript form help


does anyone know where i can find a script that will limit the user from
chosing more than a certain amount of choices in a checkbox form?  the user
can only chose up to 5 choices, but there are many choices that they can
chose from.

any help would be greatly appreciated.

Rayna

Rayna Evans
AAMC
2501 M Street, 2nd Fl
Washington, DC 20037
202-862-6243 (direct)
Extension 4243 (internal)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Structure your ColdFusion code with Fusebox. Get the official book at 
http://www.fusionauthority.com/bkinfo.cfm
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Archives: http://www.mail-archive.com/[email protected]/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists

Reply via email to