简体   繁体   中英

Restrict textbox to accept numbers and letters only in Windows 8 store app

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:

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;
        }
    }

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!

EDIT:

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.

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.";
            }
        }

    }

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.

In case you want to use Regex , Use following regex expression to match with.

^[a-zA-Z0-9]*$

Example use:

Regex r = new Regex("^[a-zA-Z0-9]*$");
if (r.IsMatch(SomeString))
{
      ...
}

My previous answer were incorrect because I made a mistake in how the VirtualKey works (it is not a char value).

Virtual keys has a defined value for any number that is 0 -9 and lower case A (a) and upper case A (A) has the same number so I am sorry if I led you in the wrong direction at first.

if(e.Key >= VirtualKey.Number0 && e.Key <= VirtualKey.Z) e.Handled = false;

This code should check that your key value is above or equal to the value for Number0 (48) and below or equal to Z | z (90).

You can read more about VirtualKey here

Enter following line in your KayPress event of textbox. It will not allow user to enter any alphabate or any digit. Only symbols will be allowed to enter:

 e.Handled = char.IsLetter(e.KeyChar) || char.IsDigit(e.KeyChar) ? false:true;

sample code:

        private void yourTextBoxName_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = char.IsLetter(e.KeyChar) || char.IsDigit(e.KeyChar) ?false:true;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM