title: Restrict textbox to accept numbers and letters only in Windows 8 store app
body: <p>I am new in programming and I'm trying to make a textbox which only accepts letters and numbers. I've searched for solutions but I can't find exactly what I want. This is what I've done so far:</p>
  
  <pre><code>private void TB1_KeyDown(object sender, KeyRoutedEventArgs e)
      {
          if (e.Key == VirtualKey.Space)
          {
              e.Handled = true;
          }
          var regex = new Regex(@"[^a-zA-Z0-9\s]");
          if (regex.IsMatch(e.Key.ToString()))
          {
              e.Handled = true;
          }
      }
  </code></pre>
  
  <p>I can remove numbers to regex to prevent the user on typing special characters but this will also restrict them in typing numbers. This textbox will be a part of windows 8 store app and I am using C#. Hope somebody can help me. Thanks!</p>
  
  <p>EDIT:</p>
  
  <p>I have to give up restricting user to type special characters in the text box 'coz I'm not getting any closer to it. What I did is I used my first code but with some changes.</p>
  
  <pre><code>private void TB1_KeyDown(object sender, KeyRoutedEventArgs e)
      {
          if (e.Key == VirtualKey.Enter)
          {
              var regex = new Regex(@"[^a-zA-Z0-9\s]");
              if (regex.IsMatch(TB1.Text))
              {
                  t1.Text = "Can't contain special characters.";
              }
          }
  
      }
  </code></pre>
  
  <p>Instead of using the keypressed in testing in "IsMatch" I used the text in the textbox. This if statement will filter if other characters aside from numbers and letters are present on the textbox so I created a textblock that will comment that there's a special character in the textbox.</p>