GCSE · Computer Science · OCR · Spec J277
Input validation
Type 'dog' when a program asks your age. With no checks, it carries on with dog and crashes. With validation, you get an error message and another go.
Computer Science · Validation
Be the awkward user
This program accepts whole-number ages from 0 to 120. Four people try it: one presses enter with nothing, one types dog, one types 250, one types 14. Step through and watch what the code does with each.
Before you step: how many error messages will appear before an age is finally accepted?
1 valid = False2 while valid == False3 age = input("How old are you?")4 if age == "" then5 print("You didn't type anything. Try again.")6 elseif age is not a whole number then7 print("Your age must be a whole number.")8 elseif age < 0 OR age > 120 then9 print("That age isn't realistic. Try again.")10 else11 valid = True12 endif13 endwhile14 print("Thanks! Age saved: " + age)
Variables
Output
Step 1: valid is a flag. It starts as False and only flips to True when an input gets past every check.
Keep your eye on valid. Nothing ends the loop except valid becoming True.
Predict the inputs
Accepted or bounced?
Same checks as before, in the same order: is it blank? Is it a whole number? Is it from 0 to 120? Sort each input.
Does the program accept this input, or show an error message and ask again?
Still to sort
Accepted — the loop ends (0)
Passes every check, so valid becomes True.
Rejected — error message, ask again (0)
Fails at least one check, so the loop goes round again.
A programmer's job is to predict what people might type. Here are six things someone could enter into the age program from the run above.
WHAT YOU'VE LEARNED
A quick recap of today's lesson.
Your program has no idea 'dog' isn't an age. Unless you write the checks that tell it.
What you need to know
- Validation means checking that data is acceptable by comparing it against a set of criteria.
- It happens before the program uses the input, to make sure the data entered is reasonable and expected.
- Your job as programmer: predict what users could enter — including pressing enter with nothing — and put checks in place.
Have a goJay says, with total confidence: 'Nobody would just press enter without typing anything. No need to check for that.' Is Jay right?
No — a blank entry is a likely input, so the program needs a check for it.
Programs have to cater for all likely inputs, not just sensible ones. Jay is planning for the user he hopes for, not the one he'll actually get.
- Asking for an age? Check that the input is a number and within a realistic range.
Have a goYour age program checks just one thing: is it a number? Which of these gets in when it shouldn't: 15, 230 or dog?
230 — it's a number, but not a realistic age.
230 passes the 'is it a number?' test, which is why it's tempting to let it in. Only a realistic-range check stops it.
- Skip validation, and someone types dog as their age: the program would crash.
- A program only asks again if it's been programmed to. Otherwise it runs on with invalid data and may crash.
- Validation usually sits inside a loop that keeps asking until the input is valid.
- The repeating is done by a while loop; the testing is often done with if statements.
- Not a whole number, or outside the allowed range? Display an error message and keep asking.
Have a goPredict: a user types 12 into a validated age program at the very first prompt. How many error messages do they see?
None. 12 passes every check, so they're asked just once.
Error messages only appear when an input fails a check. The loop doesn't make anyone answer twice if their first answer is valid.
The big picture
Validation is checking that input data is acceptable — reasonable and expected — by comparing it against a set of criteria before the program uses it. It is usually built from a while loop that keeps asking and if statements that test the input, with an error message each time the input fails.
Key points
Worked example
Problem
A quiz game asks players to choose a level from 1 to 5. Plan its validation: predict the likely inputs, decide the checks, and say what happens if a player enters nothing, then three (as a word), then 9, then 4.
⚠ Watch out
Assuming the computer will spot a silly input and ask again on its own. It won't. A program only asks again if you've written the loop and the checks that make it — without them it carries on with invalid data and may crash.
Memory hook
Ask. Check. Complain. Repeat. — ask for the input, check it with if statements, show an error message if it fails, and let the while loop go round again until it's valid.
Check yourself
Cover the page. Can you define validation, give two checks for an age input, and explain what the while loop, the if statements and the error message each do in a validation routine?
Flashcards
(11)Define validation.
When does validation happen?
What do validation checks make sure of?
What does it mean for a program to cater for all likely inputs?
Which easy-to-forget input should a validation routine handle?
Two checks for an age input?
No validation, and someone types dog as their age. What happens?
Will a program ask for input again if the input is invalid?
Which loop repeats the request until the data is valid?
What often tests the input inside a validation loop?
What should happen each time an input isn't a whole number or is out of range?
Tap any card to flip it, or use Study as deck to go through them one at a time. In the full lesson these run as a spaced-repetition deck — you rate each card Hard, Good or Easy and the tricky ones keep coming back until they stick.
Learning with Lightbulb is opening soon
You can use this lesson now. Join the waitlist and we'll let you know when the full Lightbulb experience is ready.
Keep me postedMore OCR GCSE Computer Science topics
How this lesson was checked. This OCR GCSE Computer Science (specification J277)lesson was published through Lightbulb Learning's human-designed editorial process — the educational standards, accuracy rules and publication checks it must pass were authored and approved by Philip Halpin. It passed subject-specific assessment, automated educational checks and technical publication verification before going live (publication checks completed 3 November 2026). Published pages are monitored, human spot-checking is ongoing across the lesson library, and anything found wrong is corrected or withdrawn. How our lessons are made and checked. Spotted a mistake? Email hello@lightbulblearning.co and we'll review it.