AQA GCSE Computer Science (8525), section 3.2.11 (Robust and secure programming)
What is validation?
Validation is a check that data is reasonable and acceptable before the program uses it. It does not check that the data is correct: a user could type 17 as their age when they are really 16, and it would still be valid.
Common validation checks
| Check | What it tests | Example |
|---|---|---|
| Range check | the value is between limits | a mark from 0 to 100 |
| Length check | the data has a sensible length, such as a minimum | a password with at least 8 characters |
| Presence check | something has been entered (the string is not empty) | a name field |
| Type check | the data is the right type | a whole number for an age |
Pseudo-code
REPEAT
OUTPUT "Enter a number from 1 to 10"
number ← STRING_TO_INT(USERINPUT)
UNTIL number >= 1 AND number <= 10
name ← USERINPUT
WHILE LEN(name) = 0
OUTPUT "You must enter a name"
name ← USERINPUT
ENDWHILE
Python
def get_number(low, high):
while True:
text = input(f"Enter a number from {low} to {high}: ")
if text.isdigit() and low <= int(text) <= high:
return int(text)
print("Invalid, try again")
def valid_password(password):
return len(password) >= 8
print(valid_password("secret")) # False
print(valid_password("longenough1")) # True
How a validation routine works
- Ask for the data.
- Check it against the rules.
- If it fails, show a helpful message and ask again.
- If it passes, carry on.
Validation and verification
Validation checks data is sensible. It cannot catch every mistake, so important data is sometimes also checked by the user again (such as typing an email twice).
Common mistake: saying validation makes sure data is correct. It only makes sure data is acceptable (sensible). Also, do not forget to check the boundary values: a range of 1 to 10 should accept both 1 and 10.
Exam tip: write a validation routine as a loop that keeps asking until the data is valid. State which check you are using (range, length or presence).
Check yourself
Name two types of validation check.
Any two of: range check, length check, presence check, type check.
Why can a validation check not prove that data is correct?
It only checks the data is acceptable, so incorrect values that fit the rules still pass.
Write a condition to check that a mark is in the range 0 to 100.
mark ≥ 0 AND mark ≤ 100.