Loading...
Logo
Processing Forum
Anyone know if there is a way to restrict text input to a GTextField? I only want to allow numeric related chars (0123456789.-)? I have a function defined to handle textfield events and it's getting called but I'm not sure where to go from there.

tx!

Replies(1)


Unfortunately the GTextField does not support filtered input.

I assume that you want to be able to enter floating point numbers so simply filtering on 0123456789.-+ would not be enough because the number has structure so it could start with any of these characters but then if the first character is -+ then the next character must be one of 0123456789. if the first numeric character is 0 then it should be followed by the decimal point but the decimal point can only be included once and so on

It is possible to check whether a String is a valid using the following code

Copy code
  1. // s is a String and f = float
  2. try {
  3.       f = Float.parseFloat(s);
  4. }
  5. catch (Exception excp) {
  6.       // if you get here s is not a valid float
  7. }

So you could test the value entered into a GTextField that might be a start.