This question already has an answer here:
I am using C#.NET 3.5, and I have a problem in my project. In C# Windows Application, I want to make a textbox
to accept only numbers. If user try to enter characters message should be appear like "please enter numbers only", and in another textbox it has to accept valid email id
message should appear when it is invalid. It has to show invalid user id.
use this code:
I used the TryParse that @fjdumont mentioned but in the validating event instead.
I attached this to two different text boxes with in my form initializing code.
I also added the
tb.Undo()
to back out invalid changes.You can check the Ascii value by e.keychar on KeyPress event of TextBox.
By checking the AscII value you can check for number or character.
Similarly you can write logic to check the Email ID.
You might want to try
int.TryParse(string, out int)
in theKeyPress(object, KeyPressEventArgs)
event to check for numeric values. For the other problem you could use regular expressions instead.