0

How to code NAME textbox that accepts only letters & blankspaces. Same for NUMBER textbox:

private void tbOwnerName_TextChanged(object sender, EventArgs e)
{
    /*if (!char.IsControl(e.KeyChar) && !char.IsLetter(e.KeyChar))
    {
        e.Handled = true;
        base.OnKeyPress(e);
        MessageBox.Show("Please enter Characters only");

    }*/
}
Shaharyar
  • 12,254
  • 4
  • 46
  • 66
Niteen4u
  • 31
  • 6
  • Possible duplicate of [Validation textbox in winforms](http://stackoverflow.com/questions/12866314/validation-textbox-in-winforms) – Peter Szekeli Feb 20 '16 at 07:21
  • Possible duplicate of [WinForm UI Validation](http://stackoverflow.com/questions/769184/winform-ui-validation) – Rob Feb 22 '16 at 00:15

1 Answers1

2

The proper way to do that is using regular expressions , in C# you can use REGEX class to check if an string matches a patterns that declared by regular expression.

Regex regex = new Regex(@"^[a-zA-Z0-9_ ]*$");
Match match = regex.Match("Dot 55 Perls");
if (match.Success)
{
    //do something
}

this answer might help you to find the proper regular expression for your situation.

Community
  • 1
  • 1
M Taher
  • 130
  • 2
  • 13