Lijina Lijina - 4 days ago 7
C# Question

How to make a TextBox accept only alphabetic characters?

How can I make a

TextBox
only accept alphabetic characters?

Answer

You could use the following snippet:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (!System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "^[a-zA-Z]"))
    {
        MessageBox.Show("This textbox accepts only alphabetical characters");
        textBox1.Text.Remove(textBox1.Text.Length - 1);
    }
}
Comments