2

I'm trying to restrict all characters other than English alphabets, but I'm still able to enter some naughty characters which is not good! How can I prevent that. These naughty characters not caught by my regex are - _ + = ? < > '.

private void AlphaOnlyTextBox_OnKeyDown(object sender, KeyEventArgs e)
{    
    var restrictedChars = new Regex(@"[^a-zA-Z\s]");

    var match = restrictedChars.Match(e.Key.ToString());

    // Check for a naughty character in the KeyDown event.
    if (match.Success)
    {
        // Stop the character from being entered into the control since it is illegal.
        e.Handled = true;
    }
}

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

        <TextBox Height="21"
                 Width="77" 
                 MaxLength="2"
                 KeyDown="AlphaOnlyTextBox_OnKeyDown"
                                           >
        </TextBox>
    </Grid>
</Window>
13
  • The regex seems to catch those (as tested on regexr.com). Are you sure the problem isn't elsewhere? Commented Jul 11, 2016 at 18:04
  • Yes, the problem must be somewhere else. Commented Jul 11, 2016 at 18:05
  • @rtmh You can build the question easily in WPF/C#. It doesn't work for some odd reason! Commented Jul 11, 2016 at 18:07
  • @rtmh One suggestion! Maybe it has something to do with pressing the Shift button! Because we need to press that to enter +? Commented Jul 11, 2016 at 18:11
  • I wouldn't say we can build it easily. As for that issue, look into the specifics of the KeyEventArgs::Key functions, does it return what you expect? Commented Jul 11, 2016 at 18:13

2 Answers 2

2

Try this expression:

var restrictedChars = new Regex(@"[^(\W_0-9)]+");

It will exclude everything but large and small letter characters (not depending on a spceific language).

Hope it helps!

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks but it excludes everything! Even the normal alphabets!
Make sure you are using an upper case 'W' in the expression, because a lower case 'w' might cause the problem you have with the expression in my answer.
Thanks Italy, it seems the problem is somewhere else, see the answer below.
2

After so much head-scratching I realized that for some reasons unknown to me, KeyDown event doesn't capture certain characters, but PreviewTextInput does!

    private void UIElement_OnPreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        var restrictedChars = new Regex(@"[^a-zA-Z\s]");

        var match = restrictedChars.Match(e.Text);

        // Check for a naughty character in the KeyDown event.
        if (match.Success)
        {
            // Stop the character from being entered into the control since it is illegal.
            e.Handled = true;
        }
    }

    <TextBox Height="21"
             Width="77" 
             MaxLength="2"
             PreviewTextInput="UIElement_OnPreviewTextInput"
                                       >
    </TextBox>

If you also want to disable Space button:

    <TextBox Height="21"
             Width="77" 
             MaxLength="2"
             PreviewTextInput="UIElement_OnPreviewTextInput"
             PreviewKeyDown="UIElement_OnKeyDown"
                                       >
    </TextBox>

    private void UIElement_OnKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Space)
        {
            e.Handled = true;
        }
    }

1 Comment

I knew there must have been something better, Good find. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.