How to Make a Form that Allows only X of Y choices.

By Martin T

 

Hi everyone. I have designed a web form which consists only of 15 check boxes and a submit button. is there a way to only allow 6 of the 15 boxes to be checked? then after the 6 have been check, the others grey out?
many thanks in advance.
Anthony

 

Serif forum - form help

 

 

Martin Taylor says.....

 

On each checkbox in the form, I added some code with "Attach HTML". Each one has this identical code added:

onclick="checker(this);" chkgrp="1"

The chkgrp value is just in case you have other checkboxes on the same form that don't have the restriction.

Then I added the main script to the page header, again with Attach HTML. You specify the maximum number of boxes to allow at the top of the script, but the rest needs no changing. Look in the attached wpp file to find the script itself - no point in pasting it all in here.

Hope this helps.

Martin

 

Download Martin’s checkboxlimit wpp file

Download Martin’s checkboxscript file

 

<script language="javascript">

// specify maximum number of boxes to check here

var check_max = 3;

// rest of this script can stay unchanged

var check_count = 0;

function checker(t) {

 if (t.checked) {

   check_count ++;

   if (check_count >= check_max) {

     disable_boxes(t);

   }

 } else {

   check_count --;

   if (check_count < check_max) {

     enable_boxes(t);

   }

 }

}

function disable_boxes(t) {

 var frm = t.form;

 for (var i=0; i < frm.length; i++) {

   var e = frm.elements[i];

   if (e.type == "checkbox") {

     if (e.chkgrp == t.chkgrp && e.checked == false) {

       e.disabled = true;

     }

   }

 }

}

function enable_boxes(t) {

 var frm = t.form;

 for (var i=0; i < frm.length; i++) {

   var e = frm.elements[i];

   if (e.type == "checkbox" && e.chkgrp == t.chkgrp) {

     e.disabled = false;

   }

 }

}

</script>