Skip to main content
zachneel
Participating Frequently
April 15, 2019
Question

Is it possible to restrict an edittext to only numbers?

  • April 15, 2019
  • 2 replies
  • 2199 views

Or at least a way to return an error if anything other than a number is input?

This topic has been closed for replies.

2 replies

Roland Kahlenberg
Legend
June 28, 2022

Why not use a Slider Expression as the userInput area?

Very Advanced After Effects Training | Adaptive & Responsive Toolkits | Intelligent Design Assets (IDAs) | MoGraph Design System DEV
Joost van der Hoeven
Community Expert
Community Expert
June 28, 2022

Agree with @Roland Kahlenberg , when you need a number, why not make the field a number field?

Theodoros Tziatzios
Participating Frequently
June 28, 2022

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.

 

@Joost van der Hoeven 

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.

Theodoros Tziatzios
Participating Frequently
April 15, 2019

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.

Participant
May 27, 2022

Hi @Theodoros Tziatzios, thanks for sharing, but i tried it and it didn't work

Theodoros Tziatzios
Participating Frequently
May 27, 2022

Hello Hanan,

 

Can you please share your code, to check why it's not working ?

Cheers.