How can I check if a string contains only numbers and alphabets ie. is alphanumeric?
Considering you want to check for ASCII Alphanumeric characters, Try this:
 "^[a-zA-Z0-9]*$". Use this RegEx in String.matches(Regex), it will return true if the string is alphanumeric, else it will return false.
public boolean isAlphaNumeric(String s){
    String pattern= "^[a-zA-Z0-9]*$";
    return s.matches(pattern);
}
If it will help, read this for more details about regex: http://www.vogella.com/articles/JavaRegularExpressions/article.html
In order to be unicode compatible:
^[\pL\pN]+$
where
\pL stands for any letter
\pN stands for any number
                        It's 2016 or later and things have progressed. This matches Unicode alphanumeric strings:
^[\\p{IsAlphabetic}\\p{IsDigit}]+$
See the reference (section "Classes for Unicode scripts, blocks, categories and binary properties"). There's also this answer that I found helpful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With