Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check entered value is number or not

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!!

like image 687
A.J Avatar asked Jul 25 '13 08:07

A.J


People also ask

How do you check entered value is number or not?

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.

How do you check if input is number in HTML?

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.


1 Answers

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();
                }
            }
like image 133
Ramesh Bhati Avatar answered Oct 05 '22 07:10

Ramesh Bhati