Check the entered value - number or not.

I 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 he can accept either a username or an identifier to check the value, I do not want him to accept only numbers!

+10
source share
12 answers

Use this expression only to check the number

 String regexStr = "^[0-9]*$"; if(et_number.getText().toString().trim().matches(regexStr)) { //write code here for success } else{ // write code for failure } 
+18
source

If the boolean value is true, this is a number, otherwise a 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(); } } 
+12
source

set the value of the parameter EditText proprertyType = number, which will always accept numbers as input

 android:inputType="number" 
+6
source

Try with this regex:

 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!!!!!!!!!!!^^^"); } 
+5
source

Use the TextUtils class TextUtils to do this.

 TextUtils.isDigitsOnly("gifg"); 

it will return true if only numbers exist and false if any character exists inside the string

+3
source

You should never use an exception this way. When you expect the exception to be thrown like this, you should find an alternative way to handle it.

Try using: http://developer.android.com/reference/android/widget/TextView.html#setRawInputType%28int%29

Something like this should do this:

 editText.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED); 
0
source

Maybe 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 regular expression to fit complex formats.

0
source
 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 } 
0
source
 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"); } 
0
source

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 } 
0
source

I use this function to check the three-digit Integer and Float values ​​with two positions after the decimal point. If you do not want to limit the number of digits, remove {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; } 
0
source

for activity just add inside the android: inputType = "number"

0
source

Source: https://habr.com/ru/post/950241/


All Articles