none
Only letters and numbers in a TextBox

    Question

  • Hi all,

    I have a TextBox in which I want the user to enter only letters and numbers. How do I go about this?

    Cheerio,
    Frances
    Monday, February 12, 2007 11:49 AM

Answers

  • Hello again,

    If you attach to the PreviewTextInput event of the TextBox then you can screen all of the content which the user types in.  If you want to protect against the user copy+pasting information into your text box then you also need to attach to the DataObject.Pasting event.  This doesn't seem to show up in the intellisense, but it is there!  refactoring out the actual filtering code, you get the following:

    public partial class Window1 : System.Windows.Window
    {
     public Window1()
     {
      InitializeComponent();
     }

     private
    Boolean TextAllowed(String s)
     {
      foreach (Char c in s.ToCharArray())
      {
        if (Char.IsLetterOrDigit(c) || Char.IsControl(c)) continue;
         else return false;
      }
      return true;
     }

     private void PreviewTextInputHandler(Object sender, TextCompositionEventArgs e)
     {
      e.Handled = !TextAllowed(e.Text);
     }

     private void PastingHandler(object sender, DataObjectPastingEventArgs e)
     {
      // more error handling would be needed here - this is asking for trouble!

      String s = (String) e.DataObject.GetData(typeof(String));
      if (!TextAllowed(s)) e.CancelCommand();
     }
    }

    You hook up the elements to the TextBox as shown in the xaml below

    <TextBox Text="Hello World" PreviewTextInput="PreviewTextInputHandlerDataObject.Pasting="PastingHandler"/>

    If you prefer, you could amend this to allow the operations to continue, just hacking out the characters which are not allowed.  Hope this helps.

    Monday, February 12, 2007 5:27 PM
    Moderator

All replies

  • Hi,

    Check for Ascii Values , while user presses the keys,

    Allow only 97-122 for (a to z)

    8 for Backspace

    32 for SpaceBar

    48 to 57 for Numbers 0 to 9

    and

    65 to 90 for (A to Z )

     

    Thnx

    Sidheshwar N

    Monday, February 12, 2007 12:39 PM
  • That is unfortunately so so easily done. If you implement the KeyDown-event for the TextBox, you get this:

    private void KeyDownEventHandler(object sender, KeyEventArgs e)
    {
        // do something.
    }


    The only thing you can get there is e.Key, of which you can't get the ASCII value.

    Thanks,
    Frances
    Monday, February 12, 2007 2:24 PM
  • try PreviewTextInput event
    Monday, February 12, 2007 2:57 PM
    Moderator
  • PreviewTextInput would be the right handler to use. You could filter out the chars looking at the e.Text string.

     

    HTH

    Monday, February 12, 2007 4:39 PM
    Moderator
  • Hello,

    As above and as above-above!  Attach the following code to the PreviewTextInput of your TextBox, and perform the filtering as follows:

    private void PreviewTextInputHandler(Object sender, TextCompositionEventArgs e)
    {
      foreach (Char c in
    e.Text.ToCharArray())
      {
        if (Char.IsLetterOrDigit(c) || Char.IsControl(c)) continue
    ;
        else e.Handled = true
    ;
      }
    }

    The code above rejects the text if a single character doesn't obey the condition, but allows control characters to deletes and backspaces will work as normal.  The PreviewTextInput handler doesn't fire for Paste operations though, so does anyone know how to trap those?

    Monday, February 12, 2007 4:58 PM
    Moderator
  • Hello again,

    If you attach to the PreviewTextInput event of the TextBox then you can screen all of the content which the user types in.  If you want to protect against the user copy+pasting information into your text box then you also need to attach to the DataObject.Pasting event.  This doesn't seem to show up in the intellisense, but it is there!  refactoring out the actual filtering code, you get the following:

    public partial class Window1 : System.Windows.Window
    {
     public Window1()
     {
      InitializeComponent();
     }

     private
    Boolean TextAllowed(String s)
     {
      foreach (Char c in s.ToCharArray())
      {
        if (Char.IsLetterOrDigit(c) || Char.IsControl(c)) continue;
         else return false;
      }
      return true;
     }

     private void PreviewTextInputHandler(Object sender, TextCompositionEventArgs e)
     {
      e.Handled = !TextAllowed(e.Text);
     }

     private void PastingHandler(object sender, DataObjectPastingEventArgs e)
     {
      // more error handling would be needed here - this is asking for trouble!

      String s = (String) e.DataObject.GetData(typeof(String));
      if (!TextAllowed(s)) e.CancelCommand();
     }
    }

    You hook up the elements to the TextBox as shown in the xaml below

    <TextBox Text="Hello World" PreviewTextInput="PreviewTextInputHandlerDataObject.Pasting="PastingHandler"/>

    If you prefer, you could amend this to allow the operations to continue, just hacking out the characters which are not allowed.  Hope this helps.

    Monday, February 12, 2007 5:27 PM
    Moderator
  • What I've done for now is this:

    private void TextChangedEventHandler(object sender, EventArgs events)
    {
    TextBox textBox = sender as TextBox;

    string enteredText = textBox.Text;
    string newText = "";

    for (int i = 0; i <= (enteredText.Length - 1); i++)
    {
    char currentChar = enteredTextIdea;

    if (Char.IsLetterOrDigit(currentChar))
    {
    newText += enteredTextIdea;
    }
    }

    textBox.Text = newText;
    textBox.CaretIndex = newText.Length;
    }
    Tuesday, February 13, 2007 7:58 AM
  • 对于 TextBox 或 RitchTextBox 控件,设置 PreviewTextInput 事件参数的 Handled=true 只能在未打开输入法的情况下屏蔽字符输入,在输入法打开时,Handles=true 失去作用。请问,有无办法解决?

    Tuesday, August 07, 2007 9:13 AM