Limiting the Text Length in the Text Box Widget by JS

  • Last update:June 30, 2025
  • Overview

    Version

    VersionFunctional Change
    11.0

    /

    Application Scenario

    During the actual data entry process, the invoice number may be encoded to be in a standardized format with a fixed length, for example, a phone number with exactly 11 fixed digits.

    During the data entry of such content, when the field length exceeds the fixed value, you need to set a prompt, as shown in the following figure.

    动图1.gif

    Implementation Method

    You can add an After Editing event for the text box widget, allowing you to monitor the text length in the current text box in real time.

    If the length exceeds the fixed value you set, a prompt will pop up, and only the content that meets the length requirement will be retained in the text box.

    Method One: Using a Fixed Number in JS

    Method one is used to set a fixed number directly in JS to validate the text length.

    Template Creation

    Create a general report, as shown in the following figure.

    1.png

    Widget Adding

    Add a text widget for cell A1, as shown in the following figure.

    2.png

    Event Adding

    Select cell A1, choose Widget Setting > Event, and add an After Editing event, as shown in the following figure. The event is triggered each time you enter a character. That is, each time you enter a character, the text length in the current text box is validated.

    3.png

    The JavaScript code is as follows.

    iconNote:
    When the data length to be validated changes, you can modify the number used to validate the length in JS.
    var data=this.getValue();
    //Obtain the value in the cell.
    var len=data.length;
    //Check the length of the value.
    if(len>7)
    {
    _g().setCellValue("A1",data.substr(0,7));
    //If the length of the value in the cell exceeds seven characters, the first seven characters will be truncted and reassigned to the text box.
    //A1 refers to the poistion where the widget in the report is located.
    alert("A maximum of seven characters is supported.");//The pop-up box appears.
    }

    Method Two: Using a Parameter

    Method two is used to set a template parameter, and validate the text length with the parameter in JS.

    Template Creation

    The procedure is the same as that shown in section "Template Creation" (in section "Method One: Using a Fixed Number in JS"). No further details will be provided here.

    Widget Adding

    The procedure is the same as that shown in section "Widget Adding" (in section "Method One: Using a Fixed Number in JS"). No further details will be provided here.

    Parameter Adding

    Add a template parameter maxlen and set a default value, as shown in the following figure.

    iconNote:
    When the data length to be validated changes, you can modify the default value of the template parameter.

    4.png

    Event Adding

    Select cell A1, choose Widget Setting > Event, and add an After Editing event, as shown in the following figure. The event is triggered each time you enter a character. That is, each time you enter a character, the text length in the current text box is validated.

    5.png

    The JavaScript code is as follows.

    var data=this.getValue();
    //Obtain the value in the cell.
    var len=data.length;
    //Check the length of the value.
    if(len>Number(maxlen))
    {
    _g().setCellValue("A1",data.substr(0,Number(maxlen)));
    //If the length of the value in the cell exceeds the maxlen characters (namely, seven), the first seven characters will be truncated.
    //A1 refers to the position where the widget in the report is located.
    alert("A maximum of "+Number(maxlen)+" characters is supported.");
    }

    Effect Preview

    iconNote: 
    The effect cannot be previewed on mobile terminals.

    The preview effects realized by method one and method two are the same.

    Save the template and click Data Entry Preview. The effect is the same as that shown in section "Application Scenario."

    Template Download

    For details about method one, you can download Method One_Using a Fixed Number in JS.cpt.

    For details about method two, you can download Method Two_Using a Parameter.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