Limit values that can be entered on list collector variable

JC S_
Mega Guru

We are trying to limit the number of items that can be entered on a list collector on the form of one of our record producers.

Currently user can enter multiple items on the list collector. We need to limit it to 3 items only then the list collector will not allow further additional entry. The user should still be able to delete existing entry and then if items entered is less than 3 then he can add another item. We see that this should be done through service catalog ui script however we can't think of a way to implement it - we can't make the field to be read-only when items entered is 3 because the user should still be able to delete existing entries and add up to 3 items.

1 ACCEPTED SOLUTION

Hi Sanjiv,



There's just a little part missing on setValue otherwise this works. Thanks!



Working code:


var variablename = 'variable name here';


var mylist = g_form.getValue(variablename).split(',');


var maxitem = 3;


     


      if (mylist.length>maxitem){


              var mynewlist = [];


              for (var i = 0;i<maxitem;i++)


              {


                      mynewlist.push(mylist[i]);


              }


              g_form.setValue(variablename,mylist[i]);


              g_form.showFieldMsg(variablename,'Only 3 items can be entered','error');


      }


}


View solution in original post

3 REPLIES 3

karthik120
Giga Expert

Hi Sioson,



Try below onchange client script



function onChange(control, oldValue, newValue, isLoading) {


      //Limit the number of selected options in a list collector


      //Specify the max options and variable name below


      var maxOptions = 3;


      var varName = 'users'; // replace your variable name


      var leftBucket = gel(varName + '_select_0');


      var rightBucket = gel(varName + '_select_1');


      var selectedOptions = rightBucket.options;


      if(selectedOptions.length > maxOptions){


              //Move any options with IDs greater than maxOptions back to left bucket


              var selectedIDs = [];


              var index = 0;


              for(var i = maxOptions; i < selectedOptions.length; i++){


                      selectedIDs[index] = i;


                      index++;


              }


              //Move options and sort the left bucket


              moveSelectedOptions(selectedIDs, rightBucket, leftBucket, '--None--');


              sortSelect(leftBucket);


              alert('You cannot select more than ' + maxOptions + ' options.');


      }


}




Thanks,


kh


SanjivMeher
Kilo Patron
Kilo Patron

You can write an onChange client script on the list collector.



Split the items by comma separated values.


And then remove any items beyond length of three.



For ex



var mylist = g_form.getValue('mylistcollector').split(',');



if (mylist.length>3)


{


        var mynewlist = [];


        for (var i = 0;i<3;i++)


        {


                  mynewlist.push(mylist[i])


        }


        g_form.setValue(mylist[i]);


        g_form.showFieldMsg('mylistcollector','You can only enter 3 values','error');


}



Please mark this response as correct or helpful if it assisted you with your question.

Hi Sanjiv,



There's just a little part missing on setValue otherwise this works. Thanks!



Working code:


var variablename = 'variable name here';


var mylist = g_form.getValue(variablename).split(',');


var maxitem = 3;


     


      if (mylist.length>maxitem){


              var mynewlist = [];


              for (var i = 0;i<maxitem;i++)


              {


                      mynewlist.push(mylist[i]);


              }


              g_form.setValue(variablename,mylist[i]);


              g_form.showFieldMsg(variablename,'Only 3 items can be entered','error');


      }


}