Numeric input in textfield g4p

edited September 2015 in Programming Questions

Hi guys!

Is there a way to limit the input on a textfield to enter only numbers? I'm making a kind of number field with a textfield and two buttons (plus and minus) but I don't want the user enters letters in that textfield, I want only numbers.

Help me please!

Thanks :)

Tagged:

Answers

  • Answer ✓

    Is there a way to limit the input on a textfield to enter only numbers?

    There is no library option to do this.

    You have two user options

    1) If the user is not going to type-in a number then use a GLabel instead. That way your program can make sure only numbers are used.

    2) If the user IS going to type-in numbers then you can text the textfield contents on the GEvent.CHANGED action.

    Although option 2 can be made to work although its messy.

  • Hi, thank for your help, but i found that when I convert any string different from numbers to int the value is equal to zero, so I played with that. :D Here is more or less what I did. I hope it helps.

    if ((!textfield.getText().equals("0")&&int(textfield.getText())==0)
    {
         textfield.setText("Invalid");
      } else
      {
        //do something
      }
    
Sign In or Register to comment.