I've tried this snippet, but it doesn't work
try
{
Integer.parseInt(enteredID.getText().toString());
Log.i("enteredID value", "enterdID is numeric!!!!!!!!!!!^^^");
flag=1;
} catch (NumberFormatException e) {
flag=-1;
Log.i("enteredID value", "enterdID isn't numeric!!!!!!!!!!!^^^");
}
take care that it can accept either username or id to check the value, I don't want it to accept only numbers!!
isNumeric() method is used to check whether the entered number is numeric or not. $. isNumeric() method: It is used to check whether the given argument is a numeric value or not. If it is numeric then it returns true Otherwise returns false.
Switch <input type="text" /> to <input type="number" min="0" max="125" step="1" /> . In modern browsers this will check for an numeric input without any JS.
If boolean value is true then it is number otherwise string value
boolean digitsOnly = TextUtils.isDigitsOnly(editText.getText());
or for Example
String text = edt_email.getText().toString();
boolean digitsOnly = TextUtils.isDigitsOnly(text);
if (digitsOnly) {
if (text.length() == 0) {
Toast.makeText(getApplicationContext(), "field can't be empty.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "field is int value", Toast.LENGTH_LONG).show();
}
}else {
Toast.makeText(getApplicationContext(), "Field is string value", Toast.LENGTH_LONG).show();
}
}
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