JAVAFx TextField验证十进制值 [英] JAVAFx TextField Validation Decimal value
问题描述
我尝试限制十进制数字的文本字段,我已经找到了整数验证的解决方案这里,但问题是我无法转换以下代码的十进制数,如324.23,4.3,4,2,10.43 。 (只允许1个小数点)。
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.
推荐答案
我遇到了同样的情况。我想我找到了这个问题的解决方案。正则表达式必须由一个新的替换,并由setText稍作更改。目前它运作良好。代码如下:
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);
}
}
});
这篇关于JAVAFx TextField验证十进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!