Validation is an automatic check that data is reasonable and allowed before a program uses it. In exam questions you are usually given a requirement in plain words and asked to name the check or write it. The skill is matching the wording of the requirement to the right type of check.
This is the first lesson of the validation, verification and testing module.
Which check matches which requirement?
Read the requirement and find the key word. It tells you the check.
| Requirement wording | Check | What it tests |
|---|---|---|
| “must be between 13 and 17” | Range check | Value lies within lower and upper limits |
| “at least 8 characters” | Length check | Number of characters |
| “must be a whole number” | Type check | Data is the correct data type |
| “must not be left empty” | Presence check | Something has been entered |
| “two letters then three digits” | Format check | Characters follow a set pattern |
A check digit is another validation method, used for numbers such as barcodes. It is covered with data checking in data transmission and checking.
Worked example
A fictional school club asks for a member’s age. The requirement says: “Age must be a whole number from 13 to 17 inclusive.”
Step 1, find the key words. “Whole number” is about data type. “From 13 to 17” is about limits.
Step 2, name the checks. A type check (integer) and a range check.
Step 3, write the range check. Keep asking until the value is accepted:
REPEAT
INPUT Age
IF Age < 13 OR Age > 17 THEN
OUTPUT "Age must be 13 to 17"
ENDIF
UNTIL Age >= 13 AND Age <= 17
Step 4, trace it with the inputs 20, 12, 15.
| Input | Age < 13 OR Age > 17 | Message | UNTIL true? |
|---|---|---|---|
| 20 | TRUE (20 > 17) | shown | No |
| 12 | TRUE (12 < 13) | shown | No |
| 15 | FALSE | none | Yes |
The loop ends on 15, so the program only continues with an accepted value.
A length check follows the same shape. Here the password must have at least 8 characters:
REPEAT
INPUT Password
IF LENGTH(Password) < 8 THEN
OUTPUT "At least 8 characters needed"
ENDIF
UNTIL LENGTH(Password) >= 8
The mistake to watch for
Mistaken answer: “A range check makes sure the password is long enough.”
The student remembered the word “check” but not which property it tests.
Password length is a length check, because it counts characters. A range check compares a value with limits. Before naming a check, say aloud what is being tested: the size of a value, the number of characters, the data type, whether anything was entered, or the pattern.
Check yourself
1. A form says: “The exam score must be from 0 to 100.” Name the check.
Show answer
A range check, with lower limit 0 and upper limit 100.
2. A form says: “The email box must not be left blank.” Name the check, and say whether it proves the email is real.
Show answer
A presence check. It does not prove the email is real. It only proves that something was typed.
3. A student code must be exactly “CS” followed by three digits, such as CS417. Which check is this, and does CS999 pass?
Show answer
A format check. CS999 passes, because it follows the pattern, even if no student has that code. Validation checks the pattern, not the truth.
Where does this lead next?
The next lesson shows why verification is a different job from validation. Later you will design test data for a check like this one. You can step through a loop like the one above in the restricted pseudocode trace trainer, then try the mixed practice set.
If you still pick the wrong check under time pressure, a teacher in online one-to-one Computer Science tuition can give you new requirements to classify until the key words stand out.