Lijina Lijina - 8 months ago 61
C# Question

How to make a TextBox accept only alphabetic characters?

How can I make a

only accept alphabetic characters?

Answer Source

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