Appearance
C4 · Validation
Spec reference: Section C - Programming Paradigms
Key idea: Check that data entered by a user is acceptable before processing it.
What is validation?
Validation is the process of checking that data entered into a program is acceptable - that it is of the right type, within the right range, and in the right format - before the program processes it.
Key definition
Validation - an automated check performed by a program to ensure that data entered is reasonable, complete and within acceptable limits.
Validation ≠ Verification
- Validation checks that data is acceptable (e.g. age is between 0 and 120).
- Verification checks that data was entered correctly - e.g. typing your email address twice to confirm it matches.
Validation check techniques
1. Range check
Ensures a value falls within an acceptable minimum and maximum.
IF age < 0 OR age > 120 THEN
SEND "Invalid age" TO DISPLAY
END IFExamples:
- Age must be between 0 and 120
- Score must be between 0 and 100
- Month must be between 1 and 12
2. Type check
Ensures the data is of the correct data type.
python
try:
age = int(input("Enter your age: "))
except ValueError:
print("Error: Age must be a whole number.")Examples:
- Age must be an integer, not a string like "twenty"
- Price must be a float, not text
3. Length check
Ensures a string has an acceptable number of characters.
IF LEN(password) < 8 OR LEN(password) > 20 THEN
SEND "Password must be 8-20 characters" TO DISPLAY
END IFExamples:
- Password must be between 8 and 20 characters
- UK postcode must be 6-8 characters
4. Presence check
Ensures that a required field is not left empty.
IF username = "" THEN
SEND "Username cannot be empty" TO DISPLAY
END IF5. Format check
Ensures data matches an expected pattern or structure.
# Email must contain @ and a dot
IF NOT ("@" IN email AND "." IN email) THEN
SEND "Invalid email address" TO DISPLAY
END IFExamples:
- Email must contain
@and a. - Date entered as DD/MM/YYYY
- UK phone number starts with 07 or 01
6. Lookup check
Ensures the entered value exists in a list of acceptable values.
valid_grades = ["A", "B", "C", "D", "U"]
IF grade NOT IN valid_grades THEN
SEND "Invalid grade" TO DISPLAY
END IFPost-check actions
Once a validation check fails, the program must respond appropriately:
| Response | When to use |
|---|---|
| Display an error message | Always - tell the user what went wrong |
| Re-prompt for input | Use a loop to keep asking until valid data is entered |
| Reject and move on | Log the error and skip that record (batch processing) |
| Use a default value | Replace invalid data with a safe default |
Validation loop (best practice)
REPEAT
RECEIVE age FROM KEYBOARD
IF age < 0 OR age > 120 THEN
SEND "Please enter a valid age (0-120)" TO DISPLAY
END IF
UNTIL age >= 0 AND age <= 120This keeps asking until valid data is entered - the user cannot proceed with invalid input.
Worked example: Registration form validation
python
def validate_username(username):
if len(username) < 3:
return False, "Username must be at least 3 characters"
if len(username) > 20:
return False, "Username must be no more than 20 characters"
if " " in username:
return False, "Username cannot contain spaces"
return True, "Valid"
def validate_age(age_str):
try:
age = int(age_str)
except ValueError:
return False, "Age must be a whole number"
if age < 16 or age > 100:
return False, "Age must be between 16 and 100"
return True, "Valid"
# Usage
valid, msg = validate_username("alice_99")
print(msg) # Valid
valid, msg = validate_age("abc")
print(msg) # Age must be a whole numberSummary
| Check type | What it validates |
|---|---|
| Range check | Value is between a min and max |
| Type check | Data is the correct type (int, float, string, etc.) |
| Length check | String has acceptable number of characters |
| Presence check | Field is not empty |
| Format check | Data matches a required pattern (e.g. email, date) |
| Lookup check | Value exists in a list of allowed values |
Test Yourself
Question 1 of 5
A form requires that a user's age is between 16 and 99. Which type of validation check is this?