Soham Dasgupta
Soham Dasgupta

Reputation: 5199

Winforms stop user from entering other language character than english in textbox

I have some textboxes which are mapped to primary key fields in the database and I don't want the users to write in any language other than english or numbers or undersore even if the use manually switches the input language to anything else. How can I achieve the same. I'm on .net 2.0 so no fancy stuff. Please help.

Upvotes: 0

Views: 362

Answers (1)

VladL
VladL

Reputation: 13033

You can check your input using Regex. This pattern will match any English letter [a-zA-Z]

For example Russian ю will not match

Match m = Regex.Match("ю", "[a-zA-Z]");

Handle the KeyPress event to achieve needed functionality

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        e.Handled = !Regex.Match(e.KeyChar.ToString(), "[a-zA-Z]").Success;
    }

Upvotes: 1

Related Questions