tags:

views:

16

answers:

1

Hai, I am using SmartGwt Java. To format the price using keyPressFilter, Please suggest any regular expression. It will accept only 0-9 and single decimal only. Now I am using KeyPress handler. In this if it is empty (default) set this value "0.00", using TextItem.setEmptyDisplayValue("0.00"). So First time it will accept single decimal eventhough there is another decimal present("0.00"). Give solution for this issue. Thanks in advance. Regards, Sathya.

A: 

Many different solutions are possible.

Turning an empty field into a content of "0.00" after pressing any key is surprising, perhaps even disturbing to the user; I wouldn't do that. If you're going to assist him, you have to do so thoroughly, offering the equivalent of an input editor. Otherwise, let him do his work and just check the end result when he attempts to change focus out of the field.

Regular expressions are one way to check the input; but done alone, this is a lazy and ineffective solution. You have access to the whole input text at every keystroke. You could solve your problem by checking at keyUp time whether there is already a decimal in the input text, and if so not allow another. You could do something similar to prevent entering more than 2 digits after the decimal.

Carl Smotricz