Click here to Skip to main content
15,040,212 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to validate my form so that it enter only character value in textbox?

Probably, the best thing to do is not to do form validation to do this, but to handle the characters as they are submitted instead.
If you prevent the user from entering invalid characters in the first place, then you don't need to interrupt his work flow with a message box.

There are two ways to handle it: via the TextBox.KeyPress event and use the e.KeyChar value to restrict the values that he can enter, or use the TextBox.TextChanged event to prevent any entry of invalid characters. The later is more secure in that it covers pasted data as well as typed, but a little more complex to implement.

For example:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    if (e.KeyChar >= '0' && e.KeyChar <= '9') e.Handled = true;
Will prevent the TextBox from accepting numeric keys.

There is also the MaskedTextBox which can restrict the input in many ways.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900