How can I check the value entered in EditText is aplhanumeric or not?

My app accepts user id from user as input, user id is alphanumeric, i.e. only the first character (az), the other part is numeric. How can I check for an input of this type (eg G34555)?

+2


a source to share


3 answers


I solved the problem with a simple function string

matches

String str="mystring";



str.matches("[a-zA-Z][0-9]+");

0


a source


Use a regular expression. This should do it, assuming the first letter can be upper or lower case:



 Pattern p = Pattern.compile("[a-zA-Z][0-9]+");

 Matcher m = p.matcher("some text you want");
 boolean isAlphaNum = m.matches();

      

+3


a source


http://osdir.com/ml/Android-Developers/2009-11/msg02501.html seems to be a more decent solution, it doesn't allow characters that are not accepted.

The code from the link:

InputFilter filter = new InputFilter() {
    public CharSequence filter(CharSequence source, int start, int end,
    Spanned dest, int dstart, int dend) {
        for (int i = start; i < end; i++) {
            if (!Character.isLetterOrDigit(source.charAt(i))) {
                return "";
            }
        }
        return null;
    }
};

edit.setFilters(new InputFilter[]{filter});

      

+3


a source







All Articles