I want to add validation in javafx TextField such that user should only be able to insert integer values ([0-9] and Dot ). Also user should be able to insert either B or b(for Billion) and K or k (for Thousand) and M or m( for Million). Basically it should be an amountfield. Delete and Backspace should also work.
for example :
10k should become 10,000.00 as soon as user hit k and K should not be displayed on amountfield (textfield) Similarly 10M or 10m should convert into 10,000,000.00
adsadi342fn3 or 31233123werwer or dsad342134k should not be allowed to enter in the textfield.
I have used getKeyChar method while validating TextField in case of Swing. But I need same implementation in case of JavaFx where we don't have getKeyChar method.
I have used the following method but the problem with this is it allows user to enter any value. example : sdafewr23rf
private void amountEntered() {
if (amountField != null) {
String value;
char[] charArray = amountField.getText().toCharArray();
if (charArray.length > 0)
switch (charArray[charArray.length - 1]) {
case 't':
case 'T':
value = multiplyValue(amountField.getText(), new BigDecimal(1000000000000.0));
updateAmount(value);
break;
case 'b':
case 'B':
value = multiplyValue(amountField.getText(), new BigDecimal(1000000000.0));
updateAmount(value);
break;
case 'm':
case 'M':
value = multiplyValue(amountField.getText(), new BigDecimal(1000000.0));
updateAmount(value);
break;
case 'k':
case 'K':
value = multiplyValue(amountField.getText(), new BigDecimal(1000.0));
updateAmount(value);
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '.':
case ',':
updateAmount(amountField.getText());
break;
default:
break;
}
}
}
private String multiplyValue(String number, BigDecimal multValue) {
//get rid of "," for double parsing
BigDecimal value = new BigDecimal(cleanDouble(number.substring(0, number.length() - 1)));
value = value.multiply(multValue);
return value.toPlainString();
}