Vaildate Textbox Input: A small Newbie Q.

kingheart

Member
Joined
Aug 30, 2009
Messages
11
Programming Experience
Beginner
just a newbie question.
i need to take input in text box that will be integer.
when the button is press it will check whether the input is integer.
pls,how to perform it??

can i do this using isNumeric Function?how?
thanks very much for your answer. :)
 
You're right on the money,
VB.NET:
 If IsNumeric(TextBox1.Text) Then 'Perform calculation Else 'Give error message End If
 
Why not use the TryParse method, it will check if the conversion is possible and perform the conversion in one operation.
VB.NET:
Dim num As Integer
If Integer.TryParse(TheTextBox.Text, num) Then
   'the textbox contains a string that converts to integer
   'the integer value is now in num variable

End If
You can also use the NumericUpDown control for numeric input instead of the TextBox control. I favour this.
 
Place this code in the textbox's Keypress event. It will only allow the user to press a number key or the backspace key:

If Not Char.IsDigit(e.KeyChar) Then e.Handled = True
If e.KeyChar = Chr(8) Then e.Handled = False 'allow Backspace
 
I have been looking for a way to do that for a long time now, i knew how to allow only numbers, but just didnt know how to include the backspace, thanks for the useful post.
 
Back
Top