0

I am trying to implement data validation for my program:

private boolean typeVal (TextField typeInput, String typeMessage){
    try {
        String type = (typeInput.getText());
        addButtonClicked();
        return true;
    }catch(NumberFormatException e) {
        AlertBox.display("Datatype Error", "Please check all fields are entered and are using correct datatypes");
        return false;
    }
}

This is one example of the validators that I am trying to create, the problem comes when someone enters a string of numbers. Is there anyway to make this more specific to just certain characters?

lkerr112
  • 1
  • 2

1 Answers1

1

There is no standard validator in JavaFX. But there is several JavaFX libraries that provide such functionality, and none of them contains simple password validator. So I had to implement validators by myself.

Your code isn't quite good. You add click listener in some function. You should rather add listeners in initialize() function of FXML controller. And also you should disable button when text is empty:

button.disableProperty().bind(textField.textProperty().isEmpty());
Denis Kokorin
  • 887
  • 8
  • 17
  • There is [TextFormatter](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/TextFormatter.html), but currently it behaves like Swing's [COMMIT_OR_REVERT](https://docs.oracle.com/javase/8/docs/api/javax/swing/JFormattedTextField.html#COMMIT_OR_REVERT): Invalid input is silently discarded and the last good value is restored. – VGR Apr 02 '16 at 14:16
  • 1
    Your link is broken, did you move your repository? – MikaelF Jan 31 '17 at 02:44
  • Thanks for your comment, I have updated my answer with correct link. I just did some refactoring and broke the original link. – Denis Kokorin Jan 31 '17 at 08:49