Restricting Key Presses in Textbox Component on Pavlovia

I am attempting to restrict the key presses to include only numbers and backspace for some textbox components on Pavlovia.

This issue was resolved for Python by @TParsons and @Michael here and it works great! However, it causes issues in Pavlovia (see screenshot) if I use the automatic Python to JS translator. I believe this is because of the structure of the join function (as indicated on @wakecarter 's cribsheet) but I am unsure how to alter it to make it work for Javascript.

Here is what works in Python:

weight1.text = "".join([ch.upper() for ch in weight1.text if ch.lower() in ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "backspace"]])

Here is the corresponding JS code that does not work:

var _pj;
function _pj_snippets(container) {
    function in_es6(left, right) {
        if (((right instanceof Array) || ((typeof right) === "string"))) {
            return (right.indexOf(left) > (- 1));
        } else {
            if (((right instanceof Map) || (right instanceof Set) || (right instanceof WeakMap) || (right instanceof WeakSet))) {
                return right.has(left);
            } else {
                return (left in right);
            }
        }
    }
    container["in_es6"] = in_es6;
    return container;
}
_pj = {};
_pj_snippets(_pj);
weight1.text = "".join(function () {
    var _pj_a = [], _pj_b = weight1.text;
    for (var _pj_c = 0, _pj_d = _pj_b.length; (_pj_c < _pj_d); _pj_c += 1) {
        var ch = _pj_b[_pj_c];
        if (_pj.in_es6(ch.lower(), ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "backspace"])) {
            _pj_a.push(ch.upper());
        }
    }
    return _pj_a;
}
.call(this));

Hello,

you might find the proper approach here

The syntax for the join.method differ for python [delimeter.join(array)] and JavaScript [array.join(delimeter)]

Best wishes Jens

Hello Jens,

I have tried to change the code in Java to make it fit the syntax but I can’t seem to make it work. Do you have an idea of what exactly should be put in the “each frame” component ?

I want to do the exact same thing as the author of this post.

@cameron.morin @JensBoelte I still have not been able to make it work either. Would appreciate any insight!

Hello,

this is the error weight1.text = "".join(function (). This is python-code because the python2JavaScript can not translate the python-command properly. So you have to alter the JavaScript-code manually.

Take the following example and adapt it to your needs.

const fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.join(" and ");

results in the following string: Banana and Orange and Apple and Mango.

Best wishes Jens

example taken from https://www.w3schools.com/jsref/jsref_join.asp