I have a textBox in my program that contains a string that has to meet a few requirements. I'm asking this question to figure out the best way to meet these requirements.
This string can't be NullOrEmpty and it must be completely composed of integers. The string can also contain spaces, which is my sticking point because spaces aren't integers.
This is what I'm working with (I'm aware that it may be a bit redundant at the moment):
//I test the string whenever the textBox loses focus
private void messageBox_LostFocus(object sender, RoutedEventArgs e)
{
      if (string.IsNullOrEmpty(TextBox.Text))
          ButtonEnabled = true;
      else if (Regex.IsMatch(TextBox.Text, @"^\d+$") == false)
      {
          //I think my problem is here, the second part of the if statement doesn't
          //really seem to work because it accepts characters if there is a space
          //in the string.
          if (TextBox.Text.Contains(" ") && !Regex.IsMatch(TextBox.Text, @"^\d+$"))
              ButtonEnabled = true;
          else
          {
              MessageBox.Show("Illegal character in list.", "Warning!", MessageBoxButton.OK, MessageBoxImage.Warning);
              ButtonEnabled = false;
          }
      }
      else 
          ButtonEnabled = true;
}
I got the Regex solution from this answer.
Question: How do I make it so that this textBox only accepts values like these:
"345 78" or "456"? 
 
     
    