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:
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.