Module 6: Validating
User Input
Overview
Create Write Restricting User Input
Debug
Interface Code
and Deploy
Validating Field Data
Use Visual
Studio .NET Validating Form Data
Access
Data Debug
and Deploy
Multimedia: Validating User Input
Lesson: Restricting User Input
Guidelines for Validating User Input
What Is Intrinsic Validation?
How to Use TextBox Properties
How to Use the Masked Edit Control
Guidelines for Validating User Input
Guidelines
Guidelines
Prevent users from entering invalid data whenever
possible
Guide users through the process of entering valid
data
Allow users flexibility in how and when they enter
data
Consider validation requirements when designing
your application
Place validation code in the appropriate location,
depending on the requirements of your application
What Is Intrinsic Validation?
Definition: The built-in control properties and methods that you can
use to restrict and validate user input
Common controls that provide intrinsic validation:
Control
Control Validation technique
Validation technique
RadioButton
RadioButton Restricts entry to On or Off
Restricts entry to On or Off
CheckBox
CheckBox Restricts entry to Checked or Unchecked
Restricts entry to Checked or Unchecked
CheckedListBox Provides aalist of valid entries
CheckedListBox Provides list of valid entries
ListBox
ListBox Provides aalist of valid entries (graphics and text)
Provides list of valid entries (graphics and text)
DateTimePicker
DateTimePicker Restricts entry to dates or times
Restricts entry to dates or times
MonthCalendar
MonthCalendar Restricts entry to aarange of dates
Restricts entry to range of dates
TextBox
TextBox Set properties to restrict or modify data entry
Set properties to restrict or modify data entry
How to Use TextBox Properties
You can use the following properties to restrict or
modify user input for TextBox controls:
Control
Control Validation technique
Validation technique
PasswordChar
PasswordChar Hides or masks characters entered into aatext box
Hides or masks characters entered into text box
Sets the maximum number of characters that can be entered
Sets the maximum number of characters that can be entered
MaxLength
MaxLength into aatext box
into text box
ReadOnly
ReadOnly Provides aapredetermined valid answer
Provides predetermined valid answer
CharacterCasing Sets all characters in aatext box to uppercase or lowercase
CharacterCasing Sets all characters in text box to uppercase or lowercase
How to Use the Masked Edit Control
Procedure
Procedure
Add the Masked Edit control to the Toolbox
Add the Masked Edit control to the Toolbox
Place a Masked Edit control on a form
Place a Masked Edit control on a form
Set the properties for the control
Set the properties for the control
Access and format data entered by the user
Access and format data entered by the user
Practice: Using the Masked Edit Control
Add the Masked Edit control to the
Toolbox
Add the Masked Edit control to a form
Modify the Mask property
Modify the Format property
Access and display the user data
Lesson: Validating Field Data
How to Use Boolean Functions
How to Use the ErrorProvider Component
How to Set Focus on Controls and Text
How to Modify User Input
How to Use Validation Events
How to Use Boolean Functions
Common Boolean Functions
Common Boolean Functions
Function
Function Description
Description
Returns a Boolean value indicating whether an
Returns a Boolean value indicating whether an
IsNumeric
IsNumeric expression is recognized as a number
expression is recognized as a number
Returns a Boolean value indicating whether an
Returns a Boolean value indicating whether an
IsDate
IsDate expression evaluates to a valid date
expression evaluates to a valid date
Example
Example
If IsNumeric(TextBox1.Text)
If IsNumeric(TextBox1.Text) Then
Then
MessageBox.Show("The text
MessageBox.Show("The text box contains a number.")
box contains a number.")
End If
End If
How to Use the ErrorProvider Component
Add the ErrorProvider component to a form
Available on the Windows Forms tab of the Toolbox
Call the SetError method
The first parameter specifies where the icon should appear, and the
second parameter specifies error message to display:
ErrorProvider1.SetError (Textbox1, "Please enter a valid date.")
ErrorProvider1.SetError (Textbox1, "Please enter a valid date.")
If the user enters invalid data, an error icon and message appear
on the form:
How to Set Focus on Controls and Text
Why set focus?
When a control has focus, the user can enter data for
that control by using a mouse or keyboard
When a user enters invalid data, you can keep the
focus on the appropriate control until the error is fixed
Examples
To set focus on a TextBox control, use Focus method:
TextBox1.Focus( )
TextBox1.Focus( )
To select all text within the control, use SelectAll:
TextBox1.SelectAll( )
TextBox1.SelectAll( )
How to Modify User Input
You can modify user input by using the following
functions:
Function
Function Description
Description
UCase
UCase Converts a specified string to uppercase
Converts a specified string to uppercase
LCase
LCase Converts a specified string to lowercase
Converts a specified string to lowercase
Eliminates leading and trailing spaces in a specified
Eliminates leading and trailing spaces in a specified
Trim
Trim string
string
Example
Dim LowerCase, UpperCase
Dim LowerCase, UpperCase As String
As String
LowerCase = "Hello World
LowerCase = "Hello World 1234" ' String to convert
1234" ' String to convert
UpperCase = UCase(LowerCase) ' Returns "HELLO WORLD 1234"
UpperCase = UCase(LowerCase) ' Returns "HELLO WORLD 1234"
How to Use Validation Events
Use the CausesValidation property to trigger the Validating event
Validating event
Private Sub WarehouseTextbox_Validating(. ...)
Private Sub WarehouseTextbox_Validating(. .)
If WarehouseTextbox.Text = "" Then
If WarehouseTextbox.Text = "" Then
infoErrorProvider.SetError(WarehouseTextbox, _
infoErrorProvider.SetError(WarehouseTextbox, _
"Please enter a Warehouse name.")
"Please enter a Warehouse name.")
e.Cancel = True
e.Cancel = True
End If
End If
End Sub
End Sub
Validated event
Private Sub WarehouseTextbox_Validated(. ...)
Private Sub WarehouseTextbox_Validated(. .)
infoErrorProvider.SetError(WarehouseTextbox, "")
infoErrorProvider.SetError(WarehouseTextbox, "")
End Sub
End Sub
Practice: Validating Field Data
Write code for the Validating event to
test for data
Use the ErrorProvider to alert the user
to an error
Write code for the Validated event to
reset the ErrorProvider
Test the application
Lesson: Validating Form Data
How to Validate Multiple Fields on a Form
How to Designate Accept and Cancel Buttons
Security Issues
How to Validate Multiple Fields on a Form
Provide visual cues to the user
Example
Disable the OK button until the user enters data in all
required fields
Validate all the fields on the form at one time
Example
Put all the validation code in the Click event handler
of the OK button
How to Designate Accept and Cancel Buttons
Set the AcceptButton property on the form
Set the CancelButton property on the form
Examples
Designate the OKButton button as the accept button
Designate the EscapeButton button as the cancel
button
Security Issues
Authenticating users
Verifying the current Windows user
Use the UserName property of the
SystemInformation object
Example
MessageBox.Show("Current user is "" & SystemInformation.UserName)
MessageBox.Show("Current user is & SystemInformation.UserName)
Securing your code
Review
Create Write Restricting User Input
Debug
Interface Code
and Deploy
Validating Field Data
Use Visual
Studio .NET Validating Form Data
Access
Data Debug
and Deploy
Lab 6.1: Validating User Input
Exercise 1: Validating User Input