Paul McFedries' Web Home


Using JavaScript to Restrict the Number of Check Boxes Selected


Welcome to Hooked On Phobics!

What's your phobia? (Please check no more than three):

Myrmecophobia (Fear of ants)
Peladophobia (Fear of becoming bald)
Pogonophobia (Fear of beards)
Clinophobia (Fear of going to bed)
Geniophobia (Fear of chins)
Anthophobia (Fear of flowers)
Aviatophobia (Fear of flying)
Porphyrophobia (Fear of the color purple)
Odontophobia (Fear of teeth)
Phronemophobia (Fear of thinking)
Lachanophobia (Fear of vegetables)
Phobophobia (Fear of fear)
Pantophobia (Fear of everything)


Here's the JavaScript that checks the number of activated check boxes:
<SCRIPT LANGUAGE="JavaScript">
<!--
    // Use the maxChecked variable to set the maximum number that can be checked
    var maxChecked = 3
    var totalChecked = 0

    function CountChecked(field) {
        if (field.checked)
            totalChecked += 1
        else
            totalChecked -= 1
        // 
        if (totalChecked > maxChecked) {
            alert ("You can't check more than three boxes, silly!")
            field.checked = false
            totalChecked = maxChecked
        }
    }

    function ResetCount(){
        totalChecked = 0
    }
//-->
</SCRIPT>

Copyright © 1995-2008 Paul McFedries and Logophilia Limited