3.2.11 - Data validation routines (VB.NET)
Check whether entered data meets stated rules and repeat input when it does not. Native VB.NET answers are written without Run; the optional Python demonstration explores boundary decisions.
Checking validity
Validation checks whether entered data is acceptable under a program's rules before processing continues.
Validation
Checking that data is feasible and meets specified acceptance rules, rather than proving that it is the correct real-world value.
A quiz percentage must be between 0 and 100. Entering 566 is infeasible and can be rejected. Entering 56 instead of the actual 65 passes that range check: both are feasible, so validation cannot discover that transcription mistake. Clear rules and feedback make a program more robust, but accepting a value does not prove it is accurate.
Choose checks from the task's requirements. A required name needs a presence check, a minimum-length identifier needs a length check, and a bounded ticket quantity needs a range check. These checks ask about data; authentication asks whether credentials establish identity. A password long enough to pass validation is not thereby the correct password for an account.
Under the current GCSE guidance, assume input errors do not relate to data type. Use suitable conversions for numeric input, but implementing type checks, TryParse or exception handling is not a required prerequisite for these routines.
Length, presence and range checks
Each simple validation check compares the entered value with an explicit rule.
| Check | Native condition accepting the value | What it establishes |
|---|---|---|
| Presence | name <> "" | At least one character was entered |
| Minimum length 8 | identifier.Length >= 8 | The text has at least eight characters |
| Inclusive range 1–10 | quantity >= 1 And quantity <= 10 | The number is within both allowed bounds |
A simple presence check rejects the empty string, not necessarily spaces. If a task requires non-space characters, its rule needs an appropriate additional check; do not silently treat a basic non-empty check as enforcing that extra rule.
If identifier.Length < 8 Then
Console.WriteLine("Enter at least 8 characters")
End If
If name = "" Then
Console.WriteLine("Name required")
End If
If quantity < 1 Or quantity > 10 Then
Console.WriteLine("Quantity outside range")
End If
Reject an out-of-range number using Or: it can be below the minimum or above the maximum. Accept it using And: both lower and upper limits must be met. For 1–10, test 0, 1, 10, 11: they should respectively reject, accept, accept, reject. A normal value 5 should accept; erroneous value 20 should reject. These cases test the rule, including either side of each boundary.
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account