Restrict the selection to three checkboxes

RohanDS
Tera Contributor

Hello All,

 

I have a situation where the user can select only up to 3 check boxes as shown below.

RohanDS_0-1723151044297.png

However, I wrote an onChange script for all the four checkboxes as follows:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var count = 0;
    var arr = ['data_collection', 'report_generation', 'email_tracking','other']; //here give your checbox variable names
    for (var i = 0; i < arr.length; i++) {  //get the count of the true checkboxes
        if (g_form.getBooleanValue(arr[i]) == true)
            count++;
    }
    if (count == 3) {  //if two checkboxes are checked then make readonly for rest of the checkboxes
        for (var j = 0; j < arr.length; j++) {
            if (g_form.getBooleanValue(arr[j]) != true)
                g_form.setReadOnly(arr[j], true);
        }
    } else {  //if not make editable..
        for (var k = 0; k < arr.length; k++) {
            if (g_form.getBooleanValue(arr[k]) != true)
                g_form.setReadOnly(arr[k], false);
        }
    }
}

This is working exactly the way it should be but the issue is if I select 3 options and click on the fourth one, it just locks away the selected options and if I want to unselect a checked option and select another one, that's when it is not allowing me to do so. I have to refresh my page and start over again. Does anybody knows how to fix this?

RohanDS_1-1723151297626.png

If I unselect everything, the option I tried to select becomes read only.

RohanDS_2-1723151471231.png

 

 

 

6 REPLIES 6

Allen Andreas
Administrator
Administrator

Hi,

I'm unsure if you wrote the script yourself or if you pulled it from another post on the forums as that code is similar to others that are posted here. With that said, it's unclear if you know fully what it's doing which would help us help you, but from a very high level, it's probably because you have 4 different checkbox fields and this onChange is only going to trigger off of 1 field that you specify, so were you going to make 4 different onChange client scripts? Or how were you planning to address that?


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hello Allen,

 

The script is picked from one of the solutions provided.

As mentioned, I created 4 different onChange scripts

Hi,

Thanks for confirming. Your post doesn't call this out very clearly, which is why I asked. You said you wrote an onChange client script for the 4 checkboxes, but that could be interpreted as one script...where you are checking the 4 checkboxes (which is what this script is doing).

 

In any case, when you said this:


@RohanDS wrote:

I select 3 options and click on the fourth one, it just locks away the selected options


In theory it shouldn't do anything because clicking the 4th option would already be read-only, thus it doesn't do anything when you click it. If there's still an action happening when you click on the 4th option, then there may be another issue going on.

 

Since you have newValue == '', then when the checkbox gets unchecked, the script wouldn't run.


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!