Reputation: 63
I am using validation in GWT. I am having textbox. In that field it accepts only whole numbers and decimal values not other than special characters . I used regex pattern "[0-9*]" but it does not accept decimal values. Can you give me one solution
Thanks in advance.
Upvotes: 1
Views: 4384
Reputation: 2664
You need to check for the key pressed by the user and prevent processing of invalid keys. I've done this by subclassing IntegerBox and using a KeyDownHandler to verify appropriate cases. Here is the full class.
public class IntegerTextBox extends IntegerBox {
public IntegerTextBox() {
super();
}
public void initialize() {
addKeyDownHandler(new KeyDownHandler() {
public void onKeyDown(KeyDownEvent event) {
int keyCode = event.getNativeKeyCode();
if (event.isAnyModifierKeyDown())
cancelKey();
switch (keyCode) {
case KeyCodes.KEY_LEFT:
case KeyCodes.KEY_RIGHT:
case KeyCodes.KEY_DELETE:
case KeyCodes.KEY_BACKSPACE:
case KeyCodes.KEY_ENTER:
case KeyCodes.KEY_ESCAPE:
case KeyCodes.KEY_TAB:
return;
}
String parsedInput = parseNumberKey(keyCode);
if (parsedInput.length() > 0) {
return;
}
else {
cancelKey();
}
}
});
}
/**
* Varifies that the key pressed is either a number key or a numeric pad key. Converts numeric pad keys to the string
* representation of their number values.
*
* @param keyCode - the key code.
* @return the key's number representation as a string.
*/
@SuppressWarnings("nls")
private String parseNumberKey(int keyCode) {
String result = new String();
switch (keyCode) {
case KeyCodes.KEY_ZERO:
case KeyCodes.KEY_ONE:
case KeyCodes.KEY_TWO:
case KeyCodes.KEY_THREE:
case KeyCodes.KEY_FOUR:
case KeyCodes.KEY_FIVE:
case KeyCodes.KEY_SIX:
case KeyCodes.KEY_SEVEN:
case KeyCodes.KEY_EIGHT:
case KeyCodes.KEY_NINE:
return result = String.valueOf((char) keyCode);
case KeyCodes.KEY_NUM_ZERO:
return result = "0";
case KeyCodes.KEY_NUM_ONE:
return result = "1";
case KeyCodes.KEY_NUM_TWO:
return result = "2";
case KeyCodes.KEY_NUM_THREE:
return result = "3";
case KeyCodes.KEY_NUM_FOUR:
return result = "4";
case KeyCodes.KEY_NUM_FIVE:
return result = "5";
case KeyCodes.KEY_NUM_SIX:
return result = "6";
case KeyCodes.KEY_NUM_SEVEN:
return result = "7";
case KeyCodes.KEY_NUM_EIGHT:
return result = "8";
case KeyCodes.KEY_NUM_NINE:
return result = "9";
}
return result;
}
}
Upvotes: 0
Reputation: 5289
I know this is old, but I had the same question.
I found GWT Bootstrap's BigDecimalBox in package com.github.gwtbootstrap.client.ui. It provides a BigDecimalRenderer (for display) and a BigDecimalParser (for parsing).
https://gwtbootstrap.github.io/
Upvotes: 0
Reputation: 3502
You have to subclass textBox and handle conversion in your subclass. I.e. create some "BigDecimalBox". Then use this in the object which is going to be validated.
private static final String MIN_VALUE = "0.00";
private static final String MAX_VALUE = "99999.99";
@DecimalMax(value = MAX_VALUE)
@DecimalMin(value = MIN_VALUE)
private BigDecimal mortgage;
Avoid using double for decimal values. It's a known Java trap and a no-no if you are an enterprise developper! IMO GWT should provide a BigDecimalBox by default.
Upvotes: 2
Reputation: 743
GWT has tree specific TextBox for input number : IntegerBox, LongBox and DoubleBox.
Theses textbox will validate the format of the input text and return the Integer, Long or Double Value.
Upvotes: 1