I have a JTextField where the user can type anything. What is a convenient way to check if the String of TextField.getText() does contain letters or numbers only? Basically I just want to avoid any other symbol (eg _^$. etc)
Thank you in advance
You could consider using the String method matches
which takes a regular expression as an argument and determines whether the string matches the regular expression. For example, alphanumeric can be represented by the regular expression [a-zA-Z0-9]+
which matches the patter any lower case letter or uppercase letter or number at least 1 time.
Consider the following code:
String text = "axc";
boolean match = text.matches("[a-zA-Z0-9]+");
You might want to go through this Java Tutorial on regular expressions
try this assign the value u get frm the text field to the variable "data".. and then use that regex to know if it contains letters or numbers and not caharacters
String regex = "[a-zA-Z0-9]+";
String data = "5hekkqhwj57586";
System.out.println(data.matches(regex));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.