Count Characters Inside Fields

Contents

Overview

This sample code counts the number of characters in a Text in Text area field.
Spaces in the text field are excluded from the count. The final count value is set into a Number field.

Sample Image

The number of characters are counted upon saving the record. The Number field is also un-editable when creating or editing the record.

Prepare the App

Create an App (External link) including the following fields and settings.

Field Type Field Name Field Code
Text or Text area Body body
Number Number of Characters count

Sample Code

Prepare the following JavaScript code in a text editor and navigate to the Kintone App's settings.
Upload the file into the Upload JavaScript for PC option of the JavaScript and CSS Customization settings. For more information, refer to the following article on the Kintone Help site:
Customizing an app using JavaScript and CSS (External link) .

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
/*
 * Counts the number of characters in a Text in Text area field
 *
 * Licensed under the MIT License
 */
(() => {
  'use strict';

  kintone.events.on(['app.record.create.submit', 'app.record.edit.submit', 'app.record.index.edit.submit'], (event) => {
    // Obtain characters in the text field
    const rec = event.record;
    const text = rec.body.value || '';
    // Remove spaces
    const cleanText = text.replace(/\s+/g, '');
    // Enter character count into number field
    rec.count.value = cleanText.length;

    return event;
  });
})();

After saving the settings and clicking on Update App, create a new record in the App.
Enter some characters into the Body field, and save the Record. The number of characters should be inserted into the Number of Characters field.