2

Hi in my android application I have an edittext where inputtype is person name, but for that special characters are allowed, what I need is an edittext where I need to enter only digits and letters. But I need to validate for all digits, if all digits enter means error message need to shown.

I did like this

android:digits="0123456789qwertzuiopasdfghjklyxcvbnmQWERTZUIOPASDFGHJKLYXCVBNM"

            android:inputType="textFilter"

and with this we can enter all digits also... but if user enter all digits I need to show a error message.

roshanpeter
  • 1,334
  • 3
  • 13
  • 32

3 Answers3

6

There is an in-built function in TextUtils class to check if the string contains only digits. You could use that.

if (TextUtils.isDigitsOnly(your_string)) {
//show error message
return;
}
Abhishek V
  • 12,488
  • 6
  • 51
  • 63
0

I think what you want to check is the string is Alphanumeric or not.
So you can actually use Apache Commons Lang's StringUtils

StringUtils.isAlphanumeric(editText.getText().toString())
Niko Adrianus Yuwono
  • 11,012
  • 8
  • 42
  • 64
  • so this checks whether it is alpha numeric .. but name can be only letters .. for most of the people name is only letters.. then how we will check – roshanpeter Nov 27 '15 at 06:24
  • Wait so you only want the user to enter letters only without digit? – Niko Adrianus Yuwono Nov 27 '15 at 06:51
  • For a name most of the time only letters will be there or combination of letters and numbers, numbers alone wont be there.. I need to validate the numbers alone part – roshanpeter Nov 27 '15 at 08:56
0

You can also check for all digits like

 public static boolean validateString(String input)
    {
        try {
            int i = Integer.parseInt(input);
            return false;
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return true;
        }
    }
Hitesh Bhalala
  • 2,872
  • 23
  • 40