Copy link to clipboard
Copied
Or at least a way to return an error if anything other than a number is input?
Copy link to clipboard
Copied
Yes it is possible with an onChange function and a regular expression (regEx).
For example, the following function will only allow digits [0-9] and remove any other character (after you press enter):
myEditText.onChange = function() {
this.text = this.text.replace(/[^\d]/g,'');
} // end function
The regEx value /[^\d]/g is explained like so:
/[^\d] = Match a single character not present in the list below
\d = matches a digit (equal to [0-9])
/g = All matches (don't return after first match)
So, the whole function looks for all characters -in the given EditText field- except digits from 0 to 9 and replaces them with nothing (the double single quotes '').
(This however does not include negative numbers, because it will also remove the minus character.)
You can test and learn about regular expressions (regEx) at several on-line sites like https://regex101.com
Hope this helps.
Copy link to clipboard
Copied
Hi @Theodoros Tziatzios, thanks for sharing, but i tried it and it didn't work
Copy link to clipboard
Copied
Hello Hanan,
Can you please share your code, to check why it's not working ?
Cheers.
Copy link to clipboard
Copied
var indexTxt = pnlIndex.add('edittext {properties: {name: "indexTxt"}}');
indexTxt.text = "1";
indexTxt.onChange = function(){
indexTxt.text = indexTxt.text.replace(/[^\d]/g,'');
}
Here, i tried like what u shared, but it didn't work, please help
Copy link to clipboard
Copied
Hi,
I copied and pasted your code as is, in a dummy project, and it seems to work just fine.
All non-numerical characters are deleted from the text field, once you press enter.
Maybe you want to change the "onChange" fuction to an "onChanging" one, so that the function evaluates with each keystroke and not only when you press enter. This will prevent any typing, except number characters.
Like this:
var indexTxt = pnlIndex.add('edittext {properties: {name: "indexTxt"}}');
indexTxt.text = "1";
indexTxt.onChanging = function(){
indexTxt.text = indexTxt.text.replace(/[^\d]/g,'');
}
Hope this helps.
Copy link to clipboard
Copied
Why not use a Slider Expression as the userInput area?
Copy link to clipboard
Copied
Agree with @Roland Kahlenberg , when you need a number, why not make the field a number field?
Copy link to clipboard
Copied
There are a lot of reasons not to use a slider.
One being, screen real-estate. Sliders take up A LOT of space. When you want to create a UI panel, with as small
as possible of a screen footprint, then every pixel counts.
Also limiting/controlling a text field's input range will save you a lot of headaches down the road.
Correct if I'm wrong but I'm not aware of a "number field" on extendscript/ScriptUI panels.
It might be possible with CEP panels, though.
Cheers.