I want the JTextField to accept only Letters and Numbers. But it SHOULD contain both. It should not contain not letters only nor numbers only.
Asked
Active
Viewed 3,661 times
1
-
You have to add a control on an event of the `JTextField` (like `onKeyUp` for example) and alert the user on wrong input. I usually paint the `JTextField` background in red until the input is correct (and obviously disable the submit button) – Narmer Sep 11 '14 at 08:33
3 Answers
3
You should be using a DocumentFilter
which will filter in real time, the input to the text field.
See some of the swing+jtextfield+documentfilter tagged questions for some other sources.
Here's a simple example
public class FieldFilterDemo {
public static void main(String[] args) {
JTextComponent field = getFilteredField();
JOptionPane.showMessageDialog(null, field);
}
static JTextComponent getFilteredField() {
JTextField field = new JTextField(15);
AbstractDocument doc = (AbstractDocument) field.getDocument();
doc.setDocumentFilter(new DocumentFilter() {
public void replace(FilterBypass fb, int offs, int length,
String str, AttributeSet a) throws BadLocationException {
super.replace(fb, offs, length,
str.replaceAll("[^0-9a-zA-Z]+", ""), a);
}
public void insertString(FilterBypass fb, int offs, String str,
AttributeSet a) throws BadLocationException {
super.insertString(fb, offs,
str.replaceAll("[^0-9a-zA-Z]+", ""), a);
}
});
return field;
}
}

Community
- 1
- 1

Paul Samsotha
- 205,037
- 37
- 486
- 720
2
1) Try adding a key listener to your text field. See if it helps.
Once the user has finished typing, check the values of both flags.
private boolean hasLetter = false;
private boolean hasDigit = false;
public void keyTyped(KeyEvent evt) {
char c = evt.getKeyChar();
if (Character.isLetter(c)) {
// OK
hasLetter = true;
} else if (Character.isDigit(c)) {
// OK
hasDigit = true;
} else {
// Ignore this character
evt.consume();
}
}
2) Alternatively, just accept any character and validate at the very end
when the user has finished typing. For this you can use a regular expression.
"a1b2c".matches("^(?=.*[A-Za-z])(?=.*[0-9])[A-Za-z0-9]+$")
"123".matches("^(?=.*[A-Za-z])(?=.*[0-9])[A-Za-z0-9]+$")
"abc".matches("^(?=.*[A-Za-z])(?=.*[0-9])[A-Za-z0-9]+$")

Ben Barkay
- 5,473
- 2
- 20
- 29

peter.petrov
- 38,363
- 16
- 94
- 159
-
but how can i enforce it to have both... with this snippet i can put letters only on the text field – user3167732 Sep 11 '14 at 08:40
-
Add two boolean flags as class fields to your key listener. Check for digit and letter separately and update the respective flag. When the user has finished typing check the values of both flags. – peter.petrov Sep 11 '14 at 08:45
-
now the problem here is with the "backspace". is there a way that i can validate the whole text? – user3167732 Sep 11 '14 at 08:55
-
Well... alternatively, you can accept all chars i.e. any char, and validate only at the end, when the user has finished typing. Or... you can also accept the backspace as valid. – peter.petrov Sep 11 '14 at 08:56
-
yeah that! how can i validate the whole text that it should have both numbers and letters.is there a method or class for this or should i validate it per character? – user3167732 Sep 11 '14 at 09:02
-
-
http://stackoverflow.com/questions/11533474/java-how-to-test-if-a-string-contains-both-letter-and-number – peter.petrov Sep 11 '14 at 09:04
-
that was what i was looking for....haha...thank you...i tried regex but only with ^[a-zA-Z0-9]+$ and didnt work...how can i put your comment as the answer so that i can give you credits? – user3167732 Sep 11 '14 at 09:09
-