Issue
I am making a simple score sheet and I'm at the phase where I want to let the user create a custom sheet. Obviously I don't want them to just put in any integer willy nilly so I'm trying to connect the UnararyOperator I created to the TextField, but I'm not sure how to do it the way my JavaFX project is set up.
public class MainMenuCtrl {
@FXML
private TextField playsFld;
@FXML
void initialize() {
UnaryOperator<TextFormatter.Change> playsValidator = change -> {
if (change.getText().matches("\\d{1,2,3,4,5,6}")){
return change;
} else {
change.setText("");
change.setRange(change.getRangeStart(), change.getRangeEnd());
return change;
}
};
}
}
Solution
It turns out regex is weird and the documentation doesn't always seem to be right. After playing around a bit I found:
"(0*(?:[1-9]?)|10)"
Means 1-10, but only 1 digit unless it is specifically 10
"^[1-6]?$"
Means 1-6 and only 1 digit
"(0*(?:[1-9][0-9]?)|100)"
Means 1-100 up to 3 digits but only a 1 in the hundreds place
*0 is match the preceding character 0 times. I'm pretty sure this is how you lock the number of digits
?: means the preceding character might not show up in the string. I think this makes it so you don't need 01.
[n-n] are for the characters searched. I'm pretty sure [1-9][0-9] in the above are 1-9 for the tens place and 0-9 for the ones. So you could add brackets for every place.
| is for matching any one element. So |100 is to match 100 exactly.
$ is to tell the computer that you have to match before the end.
Answered By - horribly_n00bie
Answer Checked By - Cary Denson (JavaFixing Admin)