Visual Basic 2005 Guide
Validating User Input

Introduction

Most of the programs written so far allow the user to enter some information via a text box. If your program expects the user to enter a number or a whole number, and the user enters text, the program is likely to crash. Good application development would avoid this entirely.

There are a number of strategies for checking that the user has entered valid data. One way is to use list boxes or drop down lists to make sure that the user can only select from a range of valid data. For numbers, the NumericUpDown control can be used. However, that isn't always a possibility in our programs.

Validating Input From An Input Box

Imagine that you accept user input from an input box, and are expecting them to type a particular value. If the user types in something that isn't valid, you want them to be presented with the input box until they get things right.

The following code keeps showing an input box until the user types in the word, enter. Place it in the load event of any form to see that it works.

Dim strInput As String
Do
   strInput = InputBox("Type in the password to get in!")
Loop Until strInput = "enter"

To require the user to enter a numeric value, you could adapt the code as follows,

Dim strInput As String
Do
   strInput = InputBox("Type in a numeric value to get in!")
Loop Until IsNumeric(strInput)

The IsNumeric function looks for any numeric value including negative and decimal values. Sometimes we want an integer in a particular range. The following function can be used to do this,

Private Function isWholeNumber(ByVal strToCheck As String, ByVal intMin As Integer, ByVal intMax As Integer) As Boolean
   'check for a numeric value
   If Not IsNumeric(strToCheck) Then
      isWholeNumber = False
      Exit Function
   End If
   'check for a whole number
   If CInt(strToCheck) <> Val(strToCheck) Then
      isWholeNumber = False
      Exit Function
   End If
   'check for number in range
   If CInt(strToCheck) <= intMin Or CInt(strToCheck) >= intMax Then
      isWholeNumber = False
      Exit Function
   End If
   isWholeNumber = True
End Function

There are 3 parts to this function. First the program checks that the value passed to the function is a number, if not it exits the function returning the value false. Next, the program looks for a whole number. The CInt function converts the string to an integer. If this value is the same as the string, then we have an integer. If not, we exit the functin and return false. Lastly, we check that the number is in the range of the two values passed to the function. To use this function with the code we wrote before, do the following,

Dim strInput As String
Do
   strInput = InputBox("Type in a numeric value to get in!")
Loop Until isWholeNumber(strInput, 0, 20)

In this case, a whole number, not less than 0 and not more than 20 must be entered. By having the range values as parameters in the function, we can reuse our code many times.

The Validating Event

When the user types values into text boxes, we can get our program to check them as soon as the user tries to do anything else. To test this out, add a text box and a button to a form. Double click on the text box and choose Validating from the dropdown box in the top right hand side of the code window. Type the following code in the event handler,

If Not isWholeNumber(txtHours.Text, 0, 40) Then
   e.Cancel = True
End If

This code uses the same function that we wrote earlier to test that the user has entered a valid number of hours from 0 to 40.

A Few Extra Comments

A good program would not only reject invalid data but would make sure that the user is given hints and explanations. All of the code above needs to be adapted to make sure that the user is told what they have done wrong - it can be quite frustrating trying to get around validation if you aren't kept informed of the rules.