Topic 7 · Algorithm design and problem-solving
Validation and verification
Range, length, type, presence, format, and check digit checks, plus double entry and visual checks.
Validation
Validation is an automatic check by the program that data is reasonable and meets set rules before it is accepted. It cannot tell whether the data is correct: an age of 15 passes a range check even if you are 16.
| Check | Makes sure that... | Example |
|---|---|---|
| Range | A value is between a lower and upper limit | A percentage is 0 to 100 |
| Length | Data has an exact number, or a minimum/maximum number, of characters | A password is at least 8 characters |
| Type | Data is the right data type | An age is a whole number, not text |
| Presence | Data has been entered and is not left blank | An email field is not empty |
| Format | Data follows a pattern | A date is DD/MM/YYYY; a code is two letters then four digits |
| Check digit | A code's final digit matches the one calculated from the others | ISBNs and barcodes |
A validation loop
A REPEAT ... UNTIL loop keeps asking until the input passes. This one does a range check and shows an error message each time the value is rejected.
DECLARE Mark : INTEGER
REPEAT
OUTPUT "Enter a mark from 0 to 100"
INPUT Mark
IF Mark < 0 OR Mark > 100
THEN
OUTPUT "Invalid mark, try again"
ENDIF
UNTIL Mark >= 0 AND Mark <= 100
OUTPUT "Accepted: ", MarkA length check
LENGTH() returns the number of characters in a string, so it is used for length checks.
DECLARE Password : STRING
INPUT Password
WHILE LENGTH(Password) < 8 DO
OUTPUT "Password must be at least 8 characters"
INPUT Password
ENDWHILEVerification
Verification checks that data has been copied or entered accurately, matching the original.
- Double entry: the data is entered twice and the two entries are compared, as when you type a new password twice.
- Visual check: the user reads the data on screen and compares it with the original document.
Key idea. Validation: is the data sensible? Verification: is the data the same as the original?
Key terms
- Validation
- An automated check that data is reasonable before it is accepted.
- Verification
- A check that data has been accurately copied or entered.
- Range check
- Checks a value lies between set limits.
- Double entry
- Entering data twice and comparing the two entries.
Check yourself
Which validation check ensures a field is not left blank?Show answer
A presence check.
Why can data pass validation and still be wrong?Show answer
Validation only checks data is sensible and follows the rules, not that it is the true value.
Suggest a validation check for a UK postcode such as CB2 1TN.Show answer
A format check (letters and digits in the right pattern), or a length check.