Skip to main content
Inspiring
November 21, 2014
Question

Make EditText only allow numbers?

  • November 21, 2014
  • 3 replies
  • 2105 views

Hi there,

I am trying to make an EditText only allow to input numbers, how can I create this on the following EditText field?

var w, layout; 

layout = "dialog {  \ 

    text_field: EditText { \ 

        size: [100,20] \ 

    } \ 

}"; 

w = new Window(layout); 

w.show(); 

Thanks

This topic has been closed for replies.

3 replies

Geppetto Luis
Legend
July 21, 2019

example

// DIALOG

// ======

dialog = new Window("dialog");

    dialog.text = "EDITTEXT INSERT ONLY NUMBERS";

    dialog.orientation = "column";

    dialog.alignChildren = ["center","top"];

    dialog.spacing = 10;

    dialog.margins = 16;

statictext1 = dialog.add("statictext");

    statictext1.text = "ALERT INSERT ONLY NUMBERS";

// GROUP1

// ======

group1 = dialog.add("group");

    group1.orientation = "row";

    group1.alignChildren = ["left","center"];

    group1.spacing = 10;

    group1.margins = 0;

TEST_NUMBER = group1.add("edittext");

    TEST_NUMBER.text = "";

TEST_NUMBER.addEventListener ('keydown', NumericEditKeyboardHandler);

// DIALOG

// ======

button1 = dialog.add("button");

    button1.text = "CLOSE";

    button1.justify = "center";

   

   

button1.onClick = function () {

     dialog.close();

     }

dialog.show();

/// FUNCTION

function NumericEditKeyboardHandler (event) {

    try {

        var keyIsOK = KeyIsNumeric (event) ||

  KeyIsDelete (event) ||

  KeyIsLRArrow (event) ||

  KeyIsTabEnterEscape (event);

        if (! keyIsOK) {

            //    Bad input: tell ScriptUI not to accept the keydown event

            event.preventDefault();

            /*    Notify user of invalid input: make sure NOT

       to put up an alert dialog or do anything which

                 requires user interaction, because that

                 interferes with preventing the 'default'

                 action for the keydown event */

          // app.beep();

              alert("CAUTION!!!   \nCAN YOU ENTER NUMBERS ONLY?");

        }

    }

    catch (e) {

        ; // alert ("Ack! bug in NumericEditKeyboardHandler: " + e);

    }

}

function DesmalEditKeyboardHandler (event) {

    try {

        var keyIsOK = KeyIsNumeric (event) ||

  KeyIsPeriod (event) ||

  KeyIsDelete (event) ||

  KeyIsLRArrow (event) ||

  KeyIsTabEnterEscape (event);

        if (! keyIsOK) {

            //    Bad input: tell ScriptUI not to accept the keydown event

            event.preventDefault();

            /*    Notify user of invalid input: make sure NOT

       to put up an alert dialog or do anything which

                 requires user interaction, because that

                 interferes with preventing the 'default'

                 action for the keydown event */

           // app.beep();

            alert("CAUTION!!!   \nCAN YOU ENTER NUMBERS ONLY?");

        }

    }

    catch (e) {

        ; // alert ("Ack! bug in NumericEditKeyboardHandler: " + e);

    }

}

//    key identifier functions

function KeyHasModifier (event) {

    return event.shiftKey || event.ctrlKey || event.altKey || event.metaKey;

}

function KeyIsNumeric (event) {

    return  (event.keyName >= '0') && (event.keyName <= '9') && ! KeyHasModifier (event);

}

function KeyIsPeriod (event) {

    return  (event.keyName == 'Period') && ! KeyHasModifier (event);

}

function KeyIsDelete (event) {

    //    Shift-delete is ok

    return ((event.keyName == 'Backspace') || (event.keyName == 'Delete')) && ! (event.ctrlKey);

}

function KeyIsLRArrow (event) {

    return ((event.keyName == 'Left') || (event.keyName == 'Right')) && ! (event.altKey || event.metaKey);

}

function KeyIsTabEnterEscape (event) {

    return event.keyName == 'Tab' || event.keyName == 'Enter' || event.keyName == 'Escape';

}

JJMack
Community Expert
Community Expert
November 21, 2014

X is an extremely talented Photoshop scripter where I'm just a hacker that just hacks small scripts to do what I want done.   I created a small script a while back  to tile images onto a canvas. Data required is numeric.   I don't know Photoshop ScriptUI and those that do are annoyed at Adobe for not fixing bugs they report.  So I just steal their dialogs and smash them into what I want.and  hopefully avoid the bugs.  When I  create the dialog it would accept any values. I wanted to allow only numeric data so after doing some searching I think if i remember correctly I found some code I believe Michael Hale posted to handle the keyboard. I added that code to my small script.  You may have a easier time find our how to do what you want to do looking at a small script.   Contact Sheet II.jsx is huge.  How things fit may be harder to see in there. http://www.mouseprints.net/old/dpr/PasteImageRoll.jsx  If the uses enter anything other the a number or . and some edit keys like backspace in a numeric files they receive a beep and the key is not accepted.

JJMack
Inspiring
November 21, 2014

Check out the Contact Sheet II.jsx script that comes with PS. It has key filtering stuff in it. Ignore the stuff for handling the Enter key: it's there to validate the form before processing.