Reputation: 333
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!!
Upvotes: 17
Views: 39087
Reputation:
/**
* If @param digit is null or not any of the numbers - @return false.
*/
static boolean isNumber(String digit) {
try {
Double.parseDouble(digit);
} catch (NullPointerException | NumberFormatException ignored) {
return false;
}
return true;
}
Upvotes: 0
Reputation: 1
on the activity simply add inside the android:inputType="number"
Upvotes: 0
Reputation: 383
I use this function for checking three digit Integer and Float values with two positions after decimal point. If you don't want to limit number of digits, remove the {x,x} from the regular expression.
private boolean isNumeric(String string) {
if(Pattern.matches("\\d{1,3}((\\.\\d{1,2})?)", string))
return true;
else
return false;
}
Upvotes: 0
Reputation: 969
use this method
boolean isNumber(String string) {
try {
int amount = Integer.parseInt(string);
return true;
} catch (Exception e) {
return false;
}
}
like this :
if (isNumber(input)) {
// string is int
}
Upvotes: 2
Reputation: 1016
String text = editText123.getText().toString();
try {
int num = Integer.parseInt(text);
Log.i("",num+" is a number");
} catch (NumberFormatException e) {
Log.i("",text+" is not a number");
}
Upvotes: 0
Reputation: 1249
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();
}
}
Upvotes: 23
Reputation: 455
Use TextUtils
class function for that
TextUtils.isDigitsOnly("gifg");
it will return true if only digits are there and false if any character exists inside string
Upvotes: 5
Reputation: 1
Pattern ptrn = Pattern.compile(regexStr);
if (!ptrn.matcher(et_number.getText().toString().trim()).matches()) {
//write code here for success
}else{
//write code here for success
}
Upvotes: 0
Reputation: 519
Try with this regular expression :
String regex = "-?\\d+(\\.\\d+)?";
if (enteredID.getText().toString().matches(regex)) {
Log.i("enteredID value", "enterdID is numeric!!!!!!!!!!!^^^");
flag=1;
} else {
flag=-1;
Log.i("enteredID value", "enterdID isn't numeric!!!!!!!!!!!^^^");
}
Upvotes: 5
Reputation: 694
Use this expression for validate number only
String regexStr = "^[0-9]*$";
if(et_number.getText().toString().trim().matches(regexStr))
{
//write code here for success
}
else{
// write code for failure
}
Upvotes: 21
Reputation: 29436
Perhaps something like this:
String text = enteredID.getText().toString();
if(text.matches("\\w+")){
//--words--
}else if (text.matches("\\d+")){
//--numeric--
}else {
//-- something else --
}
You can change the regex to match complex formats.
Upvotes: 0
Reputation: 4799
You should never use an exception in this way really. When you expect an exception to be thrown like this you should find an alternative way of handling it.
Try using: http://developer.android.com/reference/android/widget/TextView.html#setRawInputType%28int%29
Something like this should do it:
editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
Upvotes: 0
Reputation: 21117
set EditText proprerty inputType = number it will always take numbers as input
android:inputType="number"
Upvotes: 7