TextBox allow only numbers (gwt 1.6.4)

2,380 views
Skip to first unread message

Dominik Erbsland

unread,
Jun 26, 2009, 9:41:47 AM6/26/09
to Google Web Toolkit
I just saw that addKeyListener() is deprecated in gwt 1.6.4.
Actually I just wanted to make a listener which checks the user input
in a text box and allows only numbers.

with gwt 1.6.4 I could not figure out how to do that since I should
not use the keyboard listener anymore.
any hints how I can check the input with the KeyUpHandler of gwt
1.6.4?

thanks in advance.

Miroslav Genov

unread,
Jun 26, 2009, 2:21:14 PM6/26/09
to Google-We...@googlegroups.com
In GWT 1.6 old listener api has been deprecated and were introduced new
event system which is using handlers. More information about your case
can be taken from:

http://google-web-toolkit.googlecode.com/svn/javadoc/1.6/com/google/gwt/event/dom/client/KeyPressHandler.html

textBox.addKeyPressHandler and so on.

Wish you good luck.

Regards,
Miroslav

Dominik Erbsland

unread,
Jun 29, 2009, 8:05:30 AM6/29/09
to Google Web Toolkit
thanks for the help so far.
I am aware that there is a new handler system - I just can't figure
out a way how to make a TextBox field "numbers only".
I tried with a KeyUpHandler and a ValueChangeHandler but could not
find suitable methods to check the input for its validity.
any hints?


On Jun 26, 8:21 pm, Miroslav Genov <mgenov.j...@gmail.com> wrote:
> In GWT 1.6 old listener api has been deprecated and were introduced new
> event system which is using handlers. More information about your case
> can be taken from:
>
>  http://google-web-toolkit.googlecode.com/svn/javadoc/1.6/com/google/g...

nobody is perfect

unread,
Jun 29, 2009, 3:07:39 PM6/29/09
to Google-We...@googlegroups.com
Just implement KeyPressHandler
on the onKeyPress method check the char code and cancel the key when appropriate.

See the example at the TextBox api docs 

Regards,
Pablo

Thomas Broyer

unread,
Jun 29, 2009, 3:20:43 PM6/29/09
to Google Web Toolkit


On 29 juin, 21:07, nobody is perfect <spam4m...@gmail.com> wrote:
> Just implement KeyPressHandleron the onKeyPress method check the char code
> and cancel the key when appropriate.
>
> See the example at the TextBox api docshttp://google-web-toolkit.googlecode.com/svn/javadoc/1.6/com/google/g...

Note that "((TextBox) event.getSource()).cancelKey();" is exactly
equivalent to "event.preventDefault();"

..and I believe the "// TODO(ECC) must be tested." really means that
it *needs* to be tested (i.e. I'm almost sure that it isn't enough;
I'm almost sure you also have to deal with KeyDown and/or KeyUp).

..but I don't really understand the OP: if you (Dominik) are able to
make it work with a KeyboardListener, what is blocking you (what is
it that you don't understand) in converting the listener into a bunch
of Key*Handlers?

Eric

unread,
Jun 29, 2009, 4:38:01 PM6/29/09
to Google Web Toolkit


On Jun 29, 3:20 pm, Thomas Broyer <t.bro...@gmail.com> wrote:
> Note that "((TextBox) event.getSource()).cancelKey();" is exactly
> equivalent to "event.preventDefault();"
>
> ..and I believe the "// TODO(ECC) must be tested." really means that
> it *needs* to be tested (i.e. I'm almost sure that it isn't enough;
> I'm almost sure you also have to deal with KeyDown and/or KeyUp).

How could I write my application to recover nicely when the user
pastes
alphabetic characters into the text box? If the user selects Ctrl (or
Cmd)-V,
or right-clicks and pastes. the browser will not invoke the keyboard
handler.
I found out that IE has an onpaste event, but that isn't universal.

Eric

Thomas Broyer

unread,
Jun 29, 2009, 6:50:15 PM6/29/09
to Google Web Toolkit
That's why I think "canceling keys" is not good practice (or at least
not good enough), you should instead (or in addition, if you really
want) either flag the field as invalid or automatically fix it (e.g.
on KeyUp field.setText(fix(field.getText()) where fix() does something
like a replaceAll("[^0-9]", "")).

The best solution, of course, would be to use an <input type=number>
in an HTML5-aware browser (Opera for now). But in the mean time, a
validation framework, or auto-correction work pretty well (and
cancelling keys are just a "bonus" if you can get it to work)

Dominik Erbsland

unread,
Jun 30, 2009, 3:22:38 AM6/30/09
to Google Web Toolkit
thanks all for the help so far!
the solution of Pablo works for me. although I also have the problem
with CTRL+V. If the user inputs a string like that there is no
cancelKey() possible.
I added a second keyhandler doing this:

inputRufnummer.addKeyUpHandler(new KeyUpHandler() {

public void onKeyUp(KeyUpEvent event) {

if (!inputRufnummer.getText().matches("[0-9]*")) {
sendButton.setEnabled(false);
sendButton.setText("Not a number");
} else {
sendButton.setEnabled(true);
sendButton.setText("Send");
}
}
});

Best solution for me would be if the framework would offer a "number
only" TextBox - but for the moment this solution must suffice.

Thomas Broyer

unread,
Jun 30, 2009, 5:42:24 AM6/30/09
to Google Web Toolkit


On 30 juin, 09:22, Dominik Erbsland <derbsl...@gmail.com> wrote:
> Best solution for me would be if the framework would offer a "number
> only" TextBox - but for the moment this solution must suffice.

Have a look at the ValueSpinner widget from the Incubator:
http://code.google.com/docreader/#p=google-web-toolkit-incubator&t=Spinner

(and note that it does not prevent pasting non-numeric text, but it
recovers quite well if you do so)
Reply all
Reply to author
Forward
0 new messages