Use the control's on change / on input event (UI Toolkit Text control) with a regular expression, and give feedback through setValid; block the keystrokes for a strict experience:
// Text control "Code" > Events > On input (or On change)
var v = me.getData() || "";
var clean = v.replace(/[^0-9]/g, ""); // numeric only (letters only: /[^A-Za-z]/g)
if (clean !== v) me.setData(clean); // strip what does not fit
me.setValid(clean.length === 0 || /^[0-9]+$/.test(clean), "Digits only");
// stricter: refuse the key before it lands (attach once in the coach view / control's load event)
var input = me.context.element.querySelector("input");
input.addEventListener("keypress", function (e) {
var ch = String.fromCharCode(e.which || e.keyCode);
if (!/[0-9]/.test(ch) && !e.ctrlKey && e.which !== 8 && e.which !== 13) e.preventDefault();
});
input.addEventListener("paste", function (e) { var t = (e.clipboardData || window.clipboardData).getData("text"); if (!/^[0-9]*$/.test(t)) e.preventDefault(); });Alternatives that need no code: the Integer / Decimal controls of the UI Toolkit accept only numbers by construction; newer toolkits offer a pattern option (regex) on the Text control with a validation message; the HTML5 pattern / inputmode="numeric" attributes can be set in the control's load event for mobile keyboards. Server-side, validate again in the coach validation script - client rules can be bypassed.
References