You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson covers the specific input validation techniques required by OCR J277 Section 2.4: range check, type check, presence check, format check, length check, and lookup check. Input validation is a core part of defensive design and ensures that data entered into a program is reasonable, complete, and in the correct format before it is processed.
Input validation is the process of checking that data entered by a user (or received from another source) meets certain criteria before the program processes it. Validation does NOT check whether data is correct — it checks whether it is reasonable and acceptable.
For example, a validation check can confirm that an age is between 0 and 150, but it cannot confirm that the user has actually entered their real age.
OCR Exam Tip: Validation checks that data is reasonable — it does NOT check that data is accurate. This distinction between validation and verification is a common exam question. Validation = reasonable; Verification = accurate/correct.
A range check ensures that a value falls within an acceptable range (minimum and maximum).
OCR Pseudocode:
do
mark = int(input("Enter mark (0-100): "))
if mark < 0 OR mark > 100 then
print("Error: mark must be between 0 and 100")
endif
until mark >= 0 AND mark <= 100
Python:
while True:
mark = int(input("Enter mark (0-100): "))
if 0 <= mark <= 100:
break
print("Error: mark must be between 0 and 100")
Examples: age must be 0–150, exam mark must be 0–100, month must be 1–12.
A type check ensures that the data is of the correct data type (e.g. integer, string, real).
OCR Pseudocode:
do
ageInput = input("Enter your age: ")
if NOT ageInput.isNumeric() then
print("Error: please enter a whole number")
endif
until ageInput.isNumeric()
age = int(ageInput)
Python:
while True:
age_input = input("Enter your age: ")
if age_input.isdigit():
age = int(age_input)
break
print("Error: please enter a whole number")
Examples: age should be an integer, price should be a real number, name should contain only letters.
A presence check ensures that data has actually been entered — the field is not left blank.
OCR Pseudocode:
do
name = input("Enter your name: ")
if name == "" then
print("Error: name cannot be empty")
endif
until name != ""
Python:
while True:
name = input("Enter your name: ")
if name.strip() != "":
break
print("Error: name cannot be empty")
Examples: username must not be blank, email address must be provided.
A format check ensures that the data follows a specific pattern or format.
OCR Pseudocode:
do
date = input("Enter date (DD/MM/YYYY): ")
valid = true
if date.length != 10 then
valid = false
elseif date.substring(2, 3) != "/" OR date.substring(5, 6) != "/" then
valid = false
endif
if valid == false then
print("Error: date must be in DD/MM/YYYY format")
endif
until valid == true
Python:
import re
while True:
date = input("Enter date (DD/MM/YYYY): ")
if re.match(r"\d{2}/\d{2}/\d{4}$", date):
break
print("Error: date must be in DD/MM/YYYY format")
Examples: date in DD/MM/YYYY, email containing @ and ., postcode in correct format.
A length check ensures that the data has an acceptable number of characters.
OCR Pseudocode:
do
password = input("Enter password (8-20 characters): ")
if password.length < 8 OR password.length > 20 then
print("Error: password must be 8-20 characters")
endif
until password.length >= 8 AND password.length <= 20
Python:
while True:
password = input("Enter password (8-20 characters): ")
if 8 <= len(password) <= 20:
break
print("Error: password must be 8-20 characters")
Examples: password must be at least 8 characters, phone number must be 11 digits, postcode 6–8 characters.
A lookup check ensures that the entered value matches one of a predefined set of acceptable values.
OCR Pseudocode:
validColours = ["red", "green", "blue", "yellow"]
do
colour = input("Enter a colour (red/green/blue/yellow): ")
found = false
for i = 0 to validColours.length - 1
if colour.lower == validColours[i] then
found = true
endif
next i
if found == false then
print("Error: invalid colour. Choose red, green, blue or yellow")
endif
until found == true
Python:
valid_colours = ["red", "green", "blue", "yellow"]
while True:
colour = input("Enter a colour (red/green/blue/yellow): ").lower()
if colour in valid_colours:
break
print("Error: invalid colour. Choose red, green, blue or yellow")
Examples: country must be from a list of countries, day must be Mon–Sun, size must be S/M/L/XL.
| Check | What It Validates | Example |
|---|---|---|
| Range | Value is within min–max | Mark between 0 and 100 |
| Type | Value is the correct data type | Age is an integer |
| Presence | Field is not empty | Name is not blank |
| Format | Value follows a specific pattern | Date is DD/MM/YYYY |
| Length | Value has acceptable character count | Password is 8–20 characters |
| Lookup | Value is in a list of allowed options | Colour is red/green/blue/yellow |
flowchart TD
A[Read user input] --> B{"Presence check<br/>not empty?"}
B -- No --> R[Reject + re-prompt]
B -- Yes --> C{"Type check<br/>correct data type?"}
C -- No --> R
C -- Yes --> D{"Length check<br/>within min-max chars?"}
D -- No --> R
D -- Yes --> E{"Format check<br/>matches pattern?"}
E -- No --> R
E -- Yes --> F{"Range check<br/>within min-max value?"}
F -- No --> R
F -- Yes --> G{"Lookup check<br/>in allowed list?"}
G -- No --> R
G -- Yes --> H[Accept input — process]
R --> A
OCR Exam Tip: A very common exam question gives you a scenario and asks you to identify which validation checks should be applied. For example: "A form asks for the user's age (whole number, 0–150)." Answer: type check (must be an integer), range check (0–150), and presence check (must not be empty). Name all applicable checks and explain each one.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.