Need to restrict a list collector field up to 10 choices in on change

sumanth1
Tera Contributor

Can any one provide the script to restrict the choices to 10 in list collector variable for on change client script.

3 REPLIES 3

Community Alums
Not applicable

Karan Chhabra6
Mega Sage
Mega Sage

Hi @sumanth1 ,

 

Please use this onChange client script, on change field would be the list collector field itself

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }


    var values = g_form.getValue('<list_collector_name>').toString().split(',');

    if (values.length > 10) {

        alert('Limit exceeded - You can select upto 10 values');
        g_form.clearValue('<list_collector_name>');
        for (var i = 0; i < 10; i++) {
            g_form.setValue('<list_collector_name>', values[i]);
        }
        return false;

    }


}

 

If my answer has helped with your question, please mark it as correct and helpful

 

Thanks!

Modified this script a little to set the correct number of values needed in the field.

 var reconstructValues = [];
    var curVal = newValue;
    values = curVal.split(",");
    if (values.length > 10) {
        alert('Limit exceeded - You can select upto 10 values only');
        g_form.clearValue('<list_collector_name>');
        for (var i = 0; i < 10; i++) {
            reconstructValues.push(values[i]);
        }
        g_form.setValue('<list_collector_name>', reconstructValues);
    }