hi i am using Eclipse Rcp and i need to validate the textbox that only accept the integer value for that i have use the code
txtCapacity.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent EVT) {
if((EVT.character>='0' && EVT.character<='9')){
txtCapacity.setEditable(true);
txtCapacity.setEditable(true);
} else {
txtCapacity.setEditable(false);
System.out.println("enter only the numeric number");
}
}
});
It validates But the Problem with this one is that i cannot use the Backspace key for deleteting the number. and please tell me idea for Validating the decimal too. thanks in advance
You can use this function to validate if the number:
If the function returns a value less than zero, then it is not a valid positive number.
You should probably set a Document on your text box. You can implement a customer Document to filter for valid input to satisfy your requirements. Each time text is added or removed from your field, the Document will check if the overall input is valid.
Dont use the
KeyListener
! Use aVerifyListener
instead as this will handle paste, backspace, replace.....E.g.
Another possibility is to use the FormattedTextField-Widget from Nebular, see http://www.eclipse.org/nebula/widgets/formattedtext/formattedtext.php The advantage is that you only have to provide a pattern, there is no need to write your own listeners..
For validating if a
value
is infact decimal or not, you can simply use -