Validation and Testing · Cambridge syllabus 7.5, 7.6, 7.7, 7.8 · Lesson 1 of 3
Validation
Validation checks whether input meets rules, such as range, type, length, presence or format. It can reject impossible values but cannot guarantee that plausible data is factually correct. Choose checks matching the data requirement.
By the end, you can
- Understand why input validation is required.
- Use range checks.
- Use length checks.
- Use type checks.
- Use presence checks.
- Use format checks where appropriate.
- Use check digits.
- Write an algorithm for each type of validation check.
- Understand why verification checks are needed.
- Use a visual check.
- Use a double entry check.
Validation techniques
Validation is an automatic check that data entered is reasonable before the program uses it. It cannot tell whether the data is true — only whether it is of a sensible kind, in a sensible range and in a sensible form.
A range check tests that a number lies between a minimum and a maximum: a percentage between 0 and 100, a month between 1 and 12, an age between 0 and 120. A length check tests the number of characters: a password of at least eight, a telephone number of exactly eleven digits.
A type check tests that the data is of the right kind — that a number really is a number and not text, so that a calculation does not fail on it. A presence check tests that something was entered at all, which matters for a field that cannot be left blank.
A format check tests the pattern: an email address containing an at sign and a dot after it, a date as two digits then two digits then four, a product code as two letters followed by four digits. A check digit, described in data transmission, recalculates the last digit of a long number such as an ISBN from the others, and rejects the number if they do not match.
Each check can be written as an algorithm, usually inside a loop that repeats until the data passes. A range check: REPEAT, INPUT Mark, UNTIL Mark >= 0 AND Mark <= 100. A length check: REPEAT, INPUT Password, UNTIL LENGTH(Password) >= 8. A presence check: REPEAT, INPUT Name, UNTIL Name <> "". A type check tests that, for example, a number has no fractional part: IF Age <> ROUND(Age, 0), then output an error. Each needs a message telling the user what was wrong.
The distinction to keep clear is that validation is not verification. A date of birth of 14 March 2009 passes every validation rule there is and can still belong to the wrong person; validation stops nonsense, not mistakes that look sensible.
Worth remembering
- Range, length, type, presence, format and check digit.
- Each check can be written as a loop that repeats until the data passes.
- Validation tests whether data is reasonable, not whether it is correct.
Verification
Verification checks that data has been entered or copied accurately, so that what was stored is what was meant. It is needed because validation cannot catch a value that is sensible but wrong, such as a phone number with two digits swapped.
A visual check is the person entering the data reading it back on the screen, or comparing it with the original document, and confirming that it matches. It is quick and needs no extra software, but people miss their own mistakes, especially when tired.
A double entry check asks for the data to be entered twice, and the computer compares the two entries. If they match, the data is accepted; if not, the user must enter it again. It is the usual check when a new password is chosen, because a mistyped password cannot be seen and would lock the user out. As an algorithm: INPUT Password, INPUT Confirm, IF Password = Confirm, accept it, ELSE output a message and ask again.
Verification cannot tell whether the data is true either: the same wrong value typed twice passes a double entry check. Used together, validation and verification catch far more errors than either alone.
Worth remembering
- Verification checks that data has been entered or copied accurately.
- Visual check: read the data back and compare it with the original.
- Double entry check: enter it twice and the computer compares the two.
Worked example
An integer mark may require Mark >= 0 AND Mark <= 100. A value of 75 passes but could still be the wrong mark for that learner.
Practice
Warm-up · not marked
Check your understanding
Answer in your own words, then compare with the worked answer. Nothing here is saved or scored.
Worked answer
Presence, type and range checks: the age must be entered, must be an integer, and Age >= 11 AND Age <= 18.
Check your method, units and explanation against it.
Marked practice
Set 1 of 6 · easiest first · Checked as you go, with the worked answer. Sign in or create a free account to save your progress.