How do I validate if a text only contains alphabetic characters?
I think we can use Pattern.matches()
but I don't know the regular expression for alphabetic characters.
How do I validate if a text only contains alphabetic characters?
I think we can use Pattern.matches()
but I don't know the regular expression for alphabetic characters.
It would be better if you used JFormattedTextField
here's more info in it: http://docs.oracle.com/javase/7/docs/api/javax/swing/JFormattedTextField.html
Use a category for alphabetics, combined with
matches
, and delimiters for the beginning and end of input text, as such:Output:
Note: this also happens to prevent empty inputs. If you want to allow them, use a different quantifier than
+
, namely,*
.Finally, since you will use that against a
java.awt.TextField
, you can simply access its text with the methodgetText()
.