Zero-fill a number

This sample zero-fills a number, and sets it into another number field.

Sample Image

zerofill.gif

Sample Code

// Zero-fill a number, and set it inside a text field

(function() {
    "use strict";
    var NUMBER = 'mynumber'; //field code of number field
    var ZEROFILL = 'zerofilled_number'; //field code of text field to set result into

    function zeroFill(value, length) {
        if (value.length >= length) { return value; }
        return (new Array(length).join('0') + value).slice(-length);
    }

    var events = ["app.record.create.change." + NUMBER, 'app.record.edit.change.' + NUMBER];

    kintone.events.on(events, function(event) {
        var record = event.record;
        var changes = event.changes.field;
        var length = 8;  //number of digits for the zero-filled number
        record[ZEROFILL].value = zeroFill(changes.value, length);
        return event;
    });
}());

Notes

Make sure to match the Field code your fields, with the Field code written in the code.

 

Refer to the Tutorial page, on how to add scripts to your app.

Was this article helpful?
0 out of 0 found this helpful
Comments
Please sign in to leave a comment.