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 = False
2 while valid == False
3 age = input("How old are you?")
4 if age == "" then
5 print("You didn't type anything. Try again.")
6 elseif age is not a whole number then
7 print("Your age must be a whole number.")
8 elseif age < 0 OR age > 120 then
9 print("That age isn't realistic. Try again.")
10 else
11 valid = True
12 endif
13 endwhile
14 print("Thanks! Age saved: " + age)

Variables

age=(not asked yet)valid=False

Output

 

Step 1: valid is a flag. It starts as False and only flips to True when an input gets past every check.

1 / 24

Keep your eye on valid. Nothing ends the loop except valid becoming True.

Step 1 of 24: valid is a flag. It starts as False and only flips to True when an input gets past every check..

Exam line: A validation routine is three things working together: a while loop that keeps asking, if statements that test the input, and an error message every time the input fails.
Watch out: The program asks again ONLY because the while loop sends it back to the input line. Take the loop away and the first bad answer is used anyway.

What would really happen?

The program with no checks

Sam's program asks 'How old are you?', stores the answer and goes straight on to use it. There is no loop and there are no checks. Someone types dog.

Which is closest to what you think happens?
How sure are you?

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.

6 of 6 still to sort.

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.

Watch out: Being a number isn't the same as being acceptable. 130 is a perfectly good whole number — it's the range check that stops it.

Your turn to build one

Ticket booking: fill the gaps

A booking program asks how many tickets someone wants. Only whole numbers from 1 to 8 are allowed. Complete the routine so it keeps asking until the answer is valid, showing an error message each time it isn't.

  1. valid = FalseStart with a flag that says 'not valid yet'.
  2. missing step
Which line is step 2?

Say it in your own words

What is validation, and why bother?

Explain what validation is and why a program should validate the data a user enters. [3 marks]

0 words · your answer stays on this page and is not sent anywhere.

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

1Validation: check input against a set of criteria before the program uses it.
2Predict likely inputs — blank ones included — and put checks in place for them.
3While loop repeats the request; if statements test the input; an error message appears on every fail.
4No loop and no checks means no second chance: the program runs on with invalid data and may crash.

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.
Checking that data is acceptable by comparing it against a set of criteria.
When does validation happen?
Before the program uses the input data.
What do validation checks make sure of?
That the data entered is reasonable and expected.
What does it mean for a program to cater for all likely inputs?
The programmer predicts what users could enter and makes sure checks are in place to handle unexpected entries.
Which easy-to-forget input should a validation routine handle?
The enter key pressed with nothing typed — a blank entry.
Two checks for an age input?
Is it a number? Is it within a realistic range?
No validation, and someone types dog as their age. What happens?
The program would crash.
Will a program ask for input again if the input is invalid?
Only if it has been programmed to. Otherwise it runs on with invalid data and may crash.
Which loop repeats the request until the data is valid?
A while loop.
What often tests the input inside a validation loop?
If statements.
What should happen each time an input isn't a whole number or is out of range?
Display an error message, and keep asking until valid input is given.

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 posted

More OCR GCSE Computer Science topics

See the full OCR Computer Science curriculum →

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.