Input Validation Help

thankins

Newcomer
Joined
Sep 24, 2003
Messages
24
I am working on a program and it calls for input validation for the text that the user enters into the text box. It does not want negative numbers or strings (i.e. "abcd...").

I understand how to do the negative numbers part of the validation but not the string part of the validation.

Here is what I have so far. I thought the IsNumeric funtion would work on it but I have a run-time error in and when I enter a letter (such as ABC) it doesnt do anything.


Code:
Dim distance As Long



        'Gets the distance for medium
      If IsNumeric(txtDistance.Text) Then
        distance = Val(txtDistance.Text)
        'Validate text to make sure no negatives number or strings where 
        'entered into the text box
        If distance < 0 Then
            MessageBox.Show("Please enter positive number for " & _
            "the distance for your selected medium.", "Text Error", MessageBoxButtons.OK, _
            MessageBoxIcon.Error)
         End If
      End If


Also Does anyone know how to round numbers to just two decimal places. I am using the data entered in the textbox with some variables and then displaying the answer in a label, but when the math is complete I get 9.099990999 as an output and I just want it to be 9.09

Thanks ahead of time for the help!
 
Oh yea

Option Strict is set to ON as part of the program prerequiste so I can't have any implicit conversions! :confused:
 
If you're writing a Windows Forms app, you can use the NumericUpDown control instead of the Textbox control. That way, you don't have to code much (if at all) to validate the numeric input because it's automatically done for you. Look for the NumericUpDown control in the Toolbox.
 
Back
Top