tags:

views:

25

answers:

1

Hi, I think this may be very simple, but it swallow my time.

I am using TextItem In GWT in my JAVA Application to get the price input from the the user.

I am trying to stop allowing more than one dot (".") on text item. All my exercise are gone to fail. I am using regular expression; Try to handle the key chars on BlurHandler event of TextItem both drag me a wrong.

Can anybody help me to achieve the above? Thank you in advance.

A: 

Something like this is what I use:

amount = new TextBox();
amount.setVisibleLength(10);
amount.addKeyPressHandler(new KeyPressHandler() {
    public void onKeyPress(KeyPressEvent event) {
       if (!checkNumeric(amount.getText(), event.getCharCode()))
          amount.cancelKey();
       }
});


public boolean checkNumeric(String text, char keycode) {
    if (!Character.isDigit(keycode) && keycode != '.' && keycode != '-'
       && keycode != KeyCodes.KEY_BACKSPACE
       && keycode != KeyCodes.KEY_TAB) {
       return false;
    }
    if (keycode == '-') {
        if (!text.isEmpty()) {
            return false;
        }
    }
    if (keycode == '.') {
        if (text.contains(".")) {
            return false;
        }
    }

    return true;
}
BJB
Thanks for your replyI have tried your code. I Set the value empty value as 0.00 using the following propertyTextItem.setEmptyDisplayValue("0.00"). Initially it will set the value. But When I press a dot it will allow one dot even though one dot present already in TextItem.Can you help me more?Thank you
vaduganathan
TextItem.setEmptyDisplayValue() is not part of standard GWT, so I don't know. My guess is that the display value "0.00" is not the same as the .getText() value (which is probably ""). I'd try TextItem.setText("0.00") to initialise .
BJB