I have a TextField in javaFX where the background color changes accordingly if the content is valid or not.
Really:
987654321 1 987654321 21 0101 9 1 1701 91 1 2 4101 917 1 0 43 0801 9 178 2 0 0111 9 1 084 0
Invalid:
0101 9 1 0 1 0 3124 0314 9
Mostly:
- Only numbers
- First group of 4 or 9 digits
- If the first group is 9 digits โ just two groups in total
- If the first group is 4 digits โ three, four or five groups in total
- Group two and three digits 1-9999
- Group four and five digits 0-9999
Now think of one of these (valid) lines as "Identity."
Current current regex:
final String base = "(\\d+\\s+\\d+)|(\\d+\\s+\\d+\\s+\\d+(\\s+\\d+)?(\\s+\\d+)?)|(\\d+\\s+\\d+\\s+\\d+)|(\\d+\\s+\\d+\\s+\\d+\\s+\\d+)|(\\d+\\s+\\d+\\s+\\d+\\s+\\d+\\s+\\d+)";
Wich works fine so far, but now I want to enable csv. Therefore, I can enter only one identifier, as I'm used to, or several identifiers, separated by a comma (,), but not more than five identifiers.
My attempt:
final String pattern = String.format("(%s,?\\s*){1,5}",base);
This allows me to enter this:
- All valid lines above
- 0101 9 1, 0101 9 2, 0101 9 3
- 0101 9 1, 987654321 21, 0101 9 3, 0101 9 4
And if I enter more than 5 identities, it is incorrectly correct. But if I enter the wrong identifier 0101 9 1 1 1 1 1 1 1 1 1, it will still become valid.
Any suggestions?:)
EDIT: This is the correspondence logic:
private final Predicate<String> typingPredicate = new Predicate<String>() { @Override public boolean apply(String input) { return input.matches(pattern); } }; textField.textProperty().addListener(new ChangeListener<String>() { @Override public void changed(ObservableValue<? extends String> observableValue, String previous, String current) { if (current != null) { if (StringUtils.isEmpty(current) || typingPredicate.apply(current.trim())) { textField.getStyleClass().removeAll("invalid"); } else { textField.getStyleClass().add("invalid"); } } } });
java regex validation javafx csv
Olav Gulbrandsen Blaaflat
source share