I am try to restrict the textfield for decimal numbers, I already found the solution for integer number validation here, but the problem is that I am not able to convert the following code for decimal numbers, something like 324.23, 4.3, 4, 2, 10.43. (only 1 decimal point allow).
vendorList_textField_remaining.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
if (!newValue.matches("\\d*")) {
vendorList_textField_remaining.setText(newValue.replaceAll("[^\\d||.]", ""));
}
}
});
I am also looking for alternative solutions. Thanks.
I have met the same situation. I think I have found the a solution to this question. The regex has to been replaced by a new one and a little changes by setText. For now it works well. The code follows:
vendorList_textField_remaining.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
if (!newValue.matches("\\d*(\\.\\d*)?")) {
vendorList_textField_remaining.setText(oldValue);
}
}
});
Thanks for help. I have solved my problem with solution (1).
vendorList_textField_remaining.textProperty().addListener(new ChangeListener<String>() {
@Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
if(!newValue.matches("\\d*(\\.\\d*)?")) {
vendorList_textField_remaining.setText(oldValue);
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With