0 votes
847 views
in Coach Views by (30.6k points)

1 Answer

0 votes
by (30.6k points)

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

Related questions

0 votes
1 answer 727 views
0 votes
1 answer 1.4k views
0 votes
1 answer 1.1k views
0 votes
1 answer 928 views
0 votes
1 answer 671 views
0 votes
1 answer 1.4k views
0 votes
1 answer 1.7k views
0 votes
1 answer 1.3k views
0 votes
1 answer 515 views
asked May 15, 2016 in Coach Views by anonymous

723 questions

807 answers

98 comments

4.9k users

Join BPM Community Discord Channel

Welcome to BPM Tips Q&A, Community wiki/forum where you can ask questions and receive answers from other IBM BPM experts and members of the community. Users with 2000 points will automatically be promoted to expert level.
Created by Dosvak LLC
Our Youtube Channel
...