0

I have a regex to allow only numbers, numbers with dot and comma, if user inputs character that breaks the regex it should not be typed in:

valueTextBox.addKeyPressHandler(new KeyPressHandler() {
            @Override
            public void onKeyPress(KeyPressEvent event) {
                String input = valueTextBox.getText();
                if (!input.matches("\\d+(?:[.,]\\d+)?")) {
                }
            }
        });

For example, I typed 123 initially, then type a dot that should be ok, and I type a number to complete it like 123.123 should be ok, if I type a comma now that should not be ok.

quarks
  • 33,478
  • 73
  • 290
  • 513

1 Answers1

1

Use DomEvent#preventDefault that prevents the wrapped native event's default action.

Use GWT RegExp for GWT client side regex.


Read more about Differences between JRE and emulated classes

Find more samples on Regular Expressions and GWT

Community
  • 1
  • 1
Braj
  • 46,415
  • 5
  • 60
  • 76