Stopbyte

Count the number of letters when the focus leaves the text area field

Hi

On Kintone, I’m trying to use the Character Count sample listed on the developer network

I want this to count the number of letters when the focus leaves the text area field (instead of when I click the save button).
I tried adding the Field Change Event ( https://developer.kintone.io/hc/en-us/articles/213149017/#FieldChangeEvent ) to the sample as below, but I can’t get it to work.


(function() {
    "use strict";
    var BODY = "textareafield"; //field code of text or text area field
    var COUNT = "lettercount"; //field code of number field

    kintone.events.on(['app.record.create.submit','app.record.edit.submit','app.record.index.edit.submit','app.record.edit.change.BODY'], function (event) {

        console.log(event);
        // Obtain characters in the text field
        var rec = event.record;
        var st = rec[BODY]['value'];
        
        //Remove spaces
        var st2 = st.replace(/\s+/g, "");

        //Enter character count into number field
        rec[COUNT]['value'] = st2.length;

        return event;
    });
})();

No error appears in the console, so I don’t really know where to start on the debug.

Hello DanielK

You probably be able to do it with the Field Change event,
but the field that you chose to count the number of letters is probably not the one
that is supported to the Field Change Event.

According to the site, only these fields are supported to the Field Change event:
(https://developer.kintone.io/hc/en-us/articles/213149017/#FieldChangeEvent)
-Single choice
-Drop-down
-Check box
-Multi-choice
-User selection
-Date
-Time
-Date and time
-Single-line text
-Number
-Table

1 Like

Thank you Yuzo, I missed that limitation in the documents.
So it was an issue on the Kintone events side, rather than a plain JavaScript issue.

So if I want to count the number of characters when I leave the focus, then I have to use a Text field as a workaround.
I also realized I was specifying the change event wrongly…I was using a variable, like “app.record.edit.change.BODY” but it should be stating the string of the field code instead, like “app.record.edit.change.textfield”


(function() {
“use strict”;
//field code of text field is “textfield”
var COUNT = “lettercount”; //field code of number field

kintone.events.on([‘app.record.create.submit’,‘app.record.edit.submit’,‘app.record.index.edit.submit’,‘app.record.edit.change.textfield’], function (event) {

   console.log(event);
   // Obtain characters in the text field
   var rec = event.record;
   var st = rec[BODY]['value'];
   
   //Remove spaces
   var st2 = st.replace(/\s+/g, "");

   //Enter character count into number field
   rec[COUNT]['value'] = st2.length;

   return event;

});
})();


Thank you, it’s working now!

1 Like