• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Is it possible to restrict an edittext to only numbers?

Explorer ,
Apr 14, 2019 Apr 14, 2019

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

1.7K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 15, 2019 Apr 15, 2019

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 27, 2022 May 27, 2022

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 27, 2022 May 27, 2022

Copy link to clipboard

Copied

Hello Hanan,

 

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

Cheers.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 28, 2022 Jun 28, 2022

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 28, 2022 Jun 28, 2022

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Jun 28, 2022 Jun 28, 2022

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 28, 2022 Jun 28, 2022

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jun 28, 2022 Jun 28, 2022

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines