Input validation is an important
part of any computer application that requires
user interaction. In its broadest sense, the
concept applies to anything that the application
does to ensure that data entered by the user
is acceptable for the purposes of the application.
Input validation can take place at various times in the data
entry cycle. For example, the programmer can:
-
Constrain the user's entry of data
before it begins by providing very restricted
data input fields that permit only valid
choices. The most common way to do this
is to provide standard controls that do
not permit free keyboard entry, such as
drop-down lists, option buttons, and check
boxes.
-
Constrain the user's entry of data
at the moment that it occurs by monitoring
every keystroke for validity and rejecting
unwanted input as it's typed. For
instance, a particular entry field might
seem to the user to ignore anything but
numeric characters.
-
React to the user's entry of data
after the user is finished, accepting or
rejecting the contents of a data field when
the user attempts to leave the field or
close the screen.
Input validation can also have varying degrees of user participation.
For instance, the program can
-
Automatically correct a user's mistakes
without asking the user's opinion.
-
Warn the user of incorrect input and prompt
the user to correct the input before allowing
the user to continue with other activities.
The first subobjective for this
chapter, verifying data entered by a user at
the field level and the form level, deals mostly
with the immediate validation of user input
from the keyboard. In order to cover this objective,
we discuss the three main Keystroke events,
KeyUp, KeyDown, and KeyPress.
These events can fire at the
level of individual controls and also at the
level of the form, thus allowing two levels
of validation. An application also usually validates
field-level data when the user finishes entry
in each field and attempts to leave the field
by setting focus away from it. We also discuss
the Validate event and CausesValidation property
(new to VB6) that you can employ in this type
of validation.
The second sub-objective for this chapter, enabling or disabling
controls based on input in fields, typically involves a more global type of crossvalidation
between data entered in two or more controls. We discuss these techniques in the
section entitled "Enabling Controls Based on Input."
Contents
-
Keystroke Events at Field and Form Level
The
KeyPress Event
The
KeyUp and KeyDown Events
KeyPress
Versus KeyUp and KeyDown
Enabling
Two-Tier Validation With the Form's KeyPreview Property
-
Field-Level Validation Techniques
The
Validate Event and CausesValidation Property
The
Validate Event
The
CausesValidation Property
The
Change Event and Click Events
An
Obsolete Technique: Validation With GotFocus and LostFocus Events
-
Enabling Controls Based on Input
-
Miscellaneous Properties for Validation
MaxLength
Data-Bound
Properties