GCSE · Computer Science · AQA · Spec 8525

Data validation routines

Type 150 into an age box and a good program won't argue. It just asks again, and again, until you give it something sensible. That stubbornness has a name.

Computer Science · Programming

The routine that won't take no for an answer

One real run: a wrong answer, then a blank, then a good one. Watch where the program goes after each ENDWHILE.

Before you press Next: the first answer is 150. Which line do you think runs after the check on line 3?

1 OUTPUT "Enter your age (0 to 120)"
2 age ← USERINPUT
3 WHILE age = "" OR age < 0 OR age > 120
4 OUTPUT "Please type an age from 0 to 120"
5 age ← USERINPUT
6 ENDWHILE
7 OUTPUT "Thanks, age saved"

Variables

age=—

Output

Enter your age (0 to 120)

Step 1: The program asks the question. Nothing is stored in age yet.

1 / 12

Press Next to run one line at a time. The green line is the one running now.

Step 1 of 12: The program asks the question. Nothing is stored in age yet..

Computer Science · Validation checks

Which check catches it?

Each input below breaks exactly one rule. Pick an input, then pick the check that catches it.

Still to sort

Presence check (0)

Has anything been entered at all?

Where the line is: Presence only asks whether something is there. It doesn't care what it is.

Type check (0)

Is it the right kind of data, such as a whole number rather than text?

Where the line is: Type is about the kind of data. Format is about the pattern its characters follow.

Range check (0)

Is the value between the lower and upper limits?

Where the line is: Range checks the size of a value. Length checks how many characters it has.

Length check (0)

Does it have an allowed number of characters?

Where the line is: Length counts characters. It doesn't look at what the characters are.

Format check (0)

Does it follow the required pattern, such as DD/MM/YYYY?

Where the line is: Format checks the shape only. Data can have the right shape and still hold an impossible value.

6 of 6 still to sort.

The routine you just stepped through used two checks. There are five you should know by name.

Computer Science · Writing a routine

Your turn: finish the routine

Complete a routine that keeps asking for a password until it has at least 8 characters.

  1. password ← USERINPUTGet the first attempt before any checking.
  2. missing step
Which line is step 2?

Computer Science · Debugging

Spot the bug

This routine should keep asking until the user picks a menu option from 1 to 4. The user types 7, then 9. Which line lets them down?

A student's routine — which line goes wrong?

Computer Science · Testing

Where exactly are the edges?

Back to the age routine: an age is accepted from 0 to 120, and both 0 and 120 are allowed. Set the two ends of the accepted range, and choose whether each end is included.

Computer Science · The limit of validation

Passed the checks. So it's right?

Mia is 16. Filling in a form, she slips and types 15. The age routine (0 to 120) accepts it straight away.

Her age passed validation. Is the stored age correct?
How sure are you?

WHAT YOU'VE LEARNED

A quick recap of today's lesson.

A validation routine is a bouncer at the door: it keeps sending you back until your input follows the rules. Following the rules isn't the same as being true, though.

What you need to know

  • Data validation checks that input is sensible and follows set rules before the program uses it. Online forms and spreadsheets validate data too.
  • A validation routine uses a loop such as WHILE. Its condition is true while the input breaks a rule, so the program keeps asking until the input is valid.
  • Inside the loop, output an error message that says what's wanted, then take a new input. Without the new input the condition never changes and the loop never ends.
  • Presence check: has something been entered? Type check: is it the right kind of data? Range check: is it between the limits? Length check: does it have an allowed number of characters? Format check: does it follow the required pattern?
  • Test data comes in three kinds. Normal data is typical and should be accepted. Boundary data sits either side of an edge of the allowed range: the value right on the edge should be accepted, and the value just past it should be rejected. Erroneous data should be rejected, for example the wrong type of data.
  • Validation can't prove data is correct. A real age of 16 typed as 15 still passes every check.

The big picture

Data validation checks that input is sensible and follows set rules before a program uses it. A validation routine is a loop: it takes an input, checks it, shows a helpful error message if a rule is broken, and asks again. It only moves on once the input is valid. The common checks are presence, type, range, length and format. You prove a routine works with normal, boundary and erroneous test data. Validation catches nonsense, but it can't guarantee that data is correct.

Key points

1Valid means sensible and follows the rules. It doesn't mean correct.
2Use a loop, not an IF, so that every new attempt is checked.
3The WHILE condition describes the input you want to reject.
4Inside the loop: a helpful message, then a fresh input.
5Test the middle, right on the edges, and just past the edges.

Worked example

Problem

A cinema's booking program asks how many tickets you want. It accepts whole numbers from 1 to 10. Choose test data that would prove its validation routine works, and say what should happen for each value.

⚠ Watch out

Writing the loop condition for good input instead of bad. WHILE age >= 0 AND age <= 120 keeps asking when the age is fine, and lets 150 straight through. The WHILE condition has to be true for the input you want to reject.

🧠

Memory hook

Picture a bouncer with a rule list: no blank names, no ages over 120. The bouncer keeps sending you to the back of the queue until you meet every rule. But the bouncer only checks the rules. Whether you're telling the truth is something no rule list can check.

✓

Check yourself

A quiz app accepts scores from 0 to 50. Write the WHILE line that keeps asking while the score is out of range. Why must 50 be one of your test values?

Flashcards

(12)
What does data validation check?
That input is sensible and follows set rules, before the program uses it.
Why does a validation routine need a loop rather than a single IF?
An IF checks only once, so a second bad attempt gets through. A loop checks every new attempt.
What two things go inside a validation loop?
An error message saying what's wanted, then a new input. Without the new input the loop never ends.
Presence check
Checks that something has been entered, so a required box isn't left empty.
Type check
Checks the data is the right kind, such as a whole number rather than text.
Range check
Checks a value is between a lower and an upper limit.
Length check
Checks the input has an allowed number of characters.
Format check
Checks the input follows a required pattern, such as DD/MM/YYYY for a date.
Normal test data
Typical data that the routine should accept.
Boundary test data
Values tested on both sides of each limit. The limit itself should pass; one step beyond it should fail.
Erroneous test data
Data the routine should reject, such as a value outside the range or the wrong type.
Can data that passes validation still be wrong?
Yes. Validation checks data is sensible, not true. A real age of 16 typed as 15 still passes.

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 AQA GCSE Computer Science topics

See the full AQA Computer Science curriculum →

How this lesson was checked. This AQA GCSE Computer Science (specification 8525)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 30 September 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.