How to make sure an user inputs only double values in a given TextField?
I found a solution for integers bu I can\'t manage to use it for doubles.
You can use the java regex on doubles and replace non-number characters with numbers. In you have to specify the number of digits allowed in the integer part, for example 1,10.
textField.textProperty().addListener(new ChangeListener() {
@Override
public void changed(ObservableValue extends String> observable, String oldValue, String newValue) {
if (!newValue.matches("[0-9]{}(\\.[0-9]*)?")) {
textField.setText(newValue.replaceAll("[^\\d.]", ""));
StringBuilder aus = new StringBuilder(newValue);
boolean firstPointFound = false;
for (int i = 0; i < aus.length(); i++){
if(aus.charAt(i) == '.') {
if(!firstPointFound)
firstPointFound = true;
else
aus.deleteCharAt(i);
}
}
newValue = aus.toString();
}
}
});