Chapter 4 Readings: 4.8 Input Validation Notes
Page 1
Section 4.8: Input Validation Concepts
Input validation is the process of inspecting data that has been input to a program to make sure it is valid before it is used in a computation. While data type validation (such as using TryParse methods) ensures that entered data matches expected data types, complete input validation also requires verifying the accuracy and logical range of the entered data.
The "Garbage In, Garbage Out" (GIGO) Principle
Definition: A foundational maxim in computer science, often abbreviated as GIGO, which highlights that computers cannot distinguish between good input and bad input.
Operational Mechanism: If a user provides bad or inaccurate data as input to a program, the program will process that bad data and, as a direct result, produce bad data as output.
Core Integrity Axiom: The integrity of a program's output is only as good as the integrity of its input. Consequently, software must be designed so that bad input is never accepted.
Real-World Examples and Implications
1. Payroll System Scenario
Consider a payroll program designed to accept the number of hours worked by an employee in a given week.
If a payroll clerk accidentally enters hours instead of hours, an extraordinarily large paycheck will be generated.
Root Cause: There are fewer than hours in an entire week (specifically, hours). However, the computer is completely unaware of this factual constraint unless explicitly programmed with validation logic to catch the error. Without validation, the system processes bad data identically to good data.
2. Media-Reported "Computer Errors"
News stories occasionally report on severe billing errors—such as individuals being mistakenly charged thousands of dollars for minor purchases or receiving unauthorized, large tax refunds.
Clarification: These incidents are rarely caused by hardware failures or system faults. Instead, they are typically caused by software bugs or invalid input data read into the program without proper validation.
Input Validation Process Workflow
Inspection: Whenever input is received by a program, it must be inspected prior to processing.
Rejection & Discarding: If the input fails validation criteria, the program must discard it.
User Prompting: The program must prompt the user to enter the correct data.
Implementation: Payroll Hours Worked Validation (Part 1)
The following code segment demonstrates nested validation for employee hours worked using a TextBox named hoursWorkedTextBox and a pre-declared int variable named hours:
1 if (int.TryParse(hoursWorkedTextBox.Text, out hours))
2 {
3 if (hours > 0 && hours <= 168)
4 {
5 // Continue to process the input.
6 }
7 else
8 {
9 MessageBox.Show("Invalid number of hours entered.");
Page 2
Implementation: Payroll Hours Worked Validation (Part 2 & Detailed Analysis)
Continuation of the payroll validation code:
10 }
11 }
12 else
13 {
14 MessageBox.Show("The hours worked must be an integer.");
15 }
Structural Analysis of Payroll Validation Logic
Assumptions & Inputs:
hoursWorkedTextBox: ATextBoxcontrol used to receive input from the user.hours: An integer variable pre-declared to store the parsed result.
Outer
ifStatement (Line 1 & Lines 12–15):Line 1: Invokes
int.TryParse(hoursWorkedTextBox.Text, out hours)to verify that the user entered a valid integer.Valid Integer Flow: If the input is successfully converted to an integer, the value is assigned to
hours, and execution advances to the innerifstatement on Line 3.Invalid Data Type Flow (Lines 12–15): If conversion fails, execution falls through to the
elseblock on Line 12, displaying a pop-up via Line 14:MessageBox.Show("The hours worked must be an integer.");.
Inner
ifStatement (Lines 3–10):Line 3: Evaluates the logical expression
hours > 0 && hours <= 168to validate data accuracy.Logical Rationale:
hours > 0: A paycheck cannot be processed for hours (or negative hours) worked.hours <= 168: The maximum theoretical limit of hours in any given week is ().
Valid Range Flow (Lines 4–6): If the conditional expression evaluates to true, processing continues safely.
Out-of-Range Flow (Lines 7–10): If the input is outside this range, execution hits Line 8 and displays:
MessageBox.Show("Invalid number of hours entered.");.
Section 4.8.1: Test Score Validation
Another common validation scenario involves restricting numeric input to a specific standardized domain range, such as test scores.
Validation Rules: A valid test score must be an integer within the inclusive range of through .
Code Example: Test Score Validation
Assumptions:
Control: A
TextBoxnamedtestScoreTextBox.Variable: An
intvariable namedtestScore(pre-declared).
1 if (int.TryParse(testScoreTextBox.Text, out testScore))
2 {
3 if (testScore >= 0 && testScore <= 100)
4 {
5 // Continue to process the input.
6 }
7 else
8 {
9 MessageBox.Show("Test score must be in the range 0 − 100.");
10 }
11 }
12 else
13 {
14 MessageBox.Show("The test score must be an integer.");
15 }
Structural Analysis of Test Score Validation Logic
Outer
ifStatement (Line 1 & Lines 12–15):Executes
int.TryParse(testScoreTextBox.Text, out testScore)to ensure the input is integer-formatted.If true, stores the input in
testScoreand proceeds to Line 3.If false, transfers control to Line 14, displaying:
MessageBox.Show("The test score must be an integer.");.
Inner
ifStatement (Lines 3–10):Evaluates
testScore >= 0 && testScore <= 100to enforce the inclusive numerical range .Valid Range Flow (Lines 4–6): Program continues processing the input.
Out-of-Range Flow (Lines 7–10): Executes Line 9, displaying:
MessageBox.Show("Test score must be in the range 0 − 100.");.