How would one limit the input of text into a TextField to where the only valid characters are numbers and letters? I've seen plenty of answers, and I tried one of them, but for some reason, I can't use backspace. The class is below.
private class NoSpaceField extends TextField {
public void replaceText(int start, int end, String text) {
String old = getText();
if (text.matches("[A-Za-z0-9\b]")) {
super.replaceText(start, end, text);
}
if (getText().length() > 16)
setText(old);
positionCaret(getText().length());
}
public void replaceSelection(String text) {
String old = getText();
if (text.matches("[A-Za-z0-9\b]")) {
super.replaceSelection(text);
}
if (getText().length() > 16)
setText(old);
positionCaret(getText().length());
}
}
I am terrible at RegEx, and have no idea how to add backspace as a valid character. Also, I've used the above class (slightly modified) for a different purpose, and it works fine.