Limiting the Number of Selected Checkboxes by JS

  • Last update:June 26, 2025
  • Overview

    Problem

    When creating a data entry template for a questionnaire, you may need to limit the number of selected checkboxes within a checkbox group. For example, the number of selected checkboxes is limited to two. If you select more than two checkboxes, all the selected values or the values exceeding the limit in the checkbox group will be cleared, as shown in the following figure.

    Implementation Method

    You can obtain the value of the current cell by JS, then check the length of the value by the length attribute, and perform the necessary operation.

    Example

    Template Preparation

    Create a template, add a checkbox group widget to cell A1, and bind the custom data (A, B, C, D, and E) to the widget, as shown in the following figure.

    JS Event Adding

    The JavaScript code differs depending on whether you want to clear all selected values or only the values exceeding the limit in the checkbox group.

    Clearing All Selected Values

    Add a State Change event to the checkbox group widget, as shown in the following figure.

    The JavaScript code is as follows.

    var value = this.getValue();
    //Obtain the value of the current widget.
    if (value.length > 2) {
    //Check if the length of the value of the current cell exceeds two.
    alert("length is "+value.length+", more than the maximum length 2!");
    this.reset();
    //Reset the data, namely, to clear all the selected data.
    }

    iconNote:
    You can also use this.setValue(""); to clear the data.

    Clearing the Value Exceeding the limit in the Checkbox Group

    Add a State Change event to the checkbox group widget, as shown in the following figure.

    The JavaScript code is as follows.

    var value = this.getValue();
    //Obtain the most recently selected value of the widget.
    var oldvalue = _g().getCellValue(0, 0, 0);
    //Obtain the originally selected value in cell A1.
    if (value.length > 2) {
    alert("length is "+value.length+", more than the maximum length 2!");
    this.setValue(oldvalue);
    //Set the value of cell A1 to the originally selected value.
    }

    Effect Preview

    Clearing All the Selected Values

    Save the template, click Data Entry Preview, and select the values of three checkboxes. In this case, "more than the maximum length 2" pops up. Click OK to clear all the data.

    The following figure shows the effect on PC.

    The following figure shows the preview effect on the DataAnalyst app and the HTML5 terminal.

    Clearing the Value Exceeding the Limit in the Checkbox Group

    Save the template, click Data Entry Preview, and select the values of three checkboxes. In this case, "more than the maximum length 2" pops up. Click OK to clear the third selected data. The following figure shows the effect.

    Template Download

    Clearing all the selected values in the checkbox group:

    For details, you can download the template Limiting the Number of Selected Checkboxes (Data Entry) by JS - Example One.cpt.

    Clearing the values exceeding the limit in the checkbox group:

    For details, you can download the template Limiting the Number of Selected Checkboxes (Data Entry) by JS - Example Two.cpt.

    Attachment List


    Theme: Data Entry
    • Helpful
    • Not helpful
    • Only read

    滑鼠選中內容,快速回饋問題

    滑鼠選中存在疑惑的內容,即可快速回饋問題,我們將會跟進處理。

    不再提示

    10s後關閉

    Get
    Help
    Online Support
    Professional technical support is provided to quickly help you solve problems.
    Online support is available from 9:00-12:00 and 13:30-17:30 on weekdays.
    Page Feedback
    You can provide suggestions and feedback for the current web page.
    Pre-Sales Consultation
    Business Consultation
    Business: international@fanruan.com
    Support: support@fanruan.com
    Page Feedback
    *Problem Type
    Cannot be empty
    Problem Description
    0/1000
    Cannot be empty

    Submitted successfully

    Network busy