Niteen4u
Niteen4u

Reputation: 31

Custom validations on textbox

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

    }*/
}

Upvotes: 0

Views: 76

Answers (1)

M Taher
M Taher

Reputation: 130

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.

Upvotes: 2

Related Questions