2

I know this is a common way to ask but I just want to include white space when checking digits number, so I have this string

6031 3100 3440 0189 000

So now I want to check that value if it is contains just only the number

I came up with this solution

String Cardresult = edtCashCard.getText().toString();
if (Cardresult.matches("[0-9]+")){
   Toast.makeText(getApplicationContext(), "Good Job the strings are numbers", Toast.LENGTH_SHORT).show();

}
else{
   Toast.makeText(getApplicationContext(), "Error the string contains character", Toast.LENGTH_SHORT).show();
}

But now I know there's white space in the variable and based on the first condition, even though your variable contains numbers but since there's white space it does not count as a number it counts as string, and now I want to consider white space as a number to trigger to the first condition, is there anything that can I do with this? It very helpful me a lot

Henry
  • 165
  • 13
  • 2
    https://stackoverflow.com/questions/13973206/regex-for-field-that-allows-numbers-and-spaces – ADM Sep 09 '21 at 06:47

1 Answers1

2

As @ADM suggested in comment you can update your regex [0-9]+ with below

[0-9 ]+

so it look like

 String Cardresult = edtCashCard.getText().toString();
    if (Cardresult.matches("[0-9 ]+")){
       Toast.makeText(getApplicationContext(), "Good Job the strings are numbers", Toast.LENGTH_SHORT).show();
    
    }
    else{
       Toast.makeText(getApplicationContext(), "Error the string contains character", Toast.LENGTH_SHORT).show();
    }
Omkar
  • 3,040
  • 1
  • 22
  • 42