Study Guide

Validation and Verification

How a program can notice data that is impossible or mistyped: validation checks (range, length, type, presence, format and check digit), verification checks (visual and double entry), and how to write validation in pseudocode.

Lesson 5.12 Algorithms and Programming

For: Cambridge IGCSE Computer Science learners, or Key Stage 3 learners who can write loops and selection (about ages 13 to 14)

Stage: Think

Three mistakes on one form

Riverside School (a fictional school) is booking a trip, and a parent fills in an online form for Mia Khan, who is 14. Three different people make a mistake in the box labelled “Pupil’s age”. Ali types 140. Ben types fourteen, in words. Cara types 15.

Think it through

The program does not know Mia’s real age. Which of the three mistakes could it still notice, and which could it not? Explain why.

Show a model answer

It could notice 140 and “fourteen”. 140 is not a possible age for a pupil, and “fourteen” is words rather than a number the program can use, so both break a rule about what is allowed. It could not notice 15: that is a perfectly possible age, so nothing about it looks wrong. Only someone who knows Mia’s real age, or a second copy of it to compare with, could spot it.

Stage: Discover

Rules about what is allowed

The trip is for pupils aged 11 to 18. The booking site also asks the parent to choose a password, which must be from 8 to 12 characters long.

Think it through

Write each of these two rules as a comparison a program could test, using AND. Use the variable names Age and Password.

Show a model answer

Age >= 11 AND Age <= 18, and LENGTH(Password) >= 8 AND LENGTH(Password) <= 12.

A check that the program makes on data as it is typed in, to see whether the data is sensible and allowed, is validation. A program that validates its input refuses data that breaks a rule and asks again. There are several kinds of rule. Two of them are the ones you have just written:

  • A range check tests that a value lies between the lowest and the highest allowed. The age rule is a range check, and 11 and 18 themselves are allowed.
  • A length check tests that the data has an allowed number of characters. It can ask for an exact length (a code that is always 6 characters) or for a minimum and maximum, as the password rule does.
INPUT Age
IF Age >= 11 AND Age <= 18
  THEN
    OUTPUT "Age accepted"
  ELSE
    OUTPUT "Age must be from 11 to 18"
ENDIF

Check your understanding

The range check allows ages from 11 to 18. Which typed age passes it?
Show the answer

Answer: C. 18

A range check has two limits, and each limit is itself allowed when the comparison is >= or <=.

Check your understanding

The password rule allows 8 to 12 characters. Does the password Maths2026! pass the length check?
Show the answer

Answer: A. Yes: it has 10 characters, which is from 8 to 12.

A length check counts characters of every kind.

Stage: Discover

The right kind of data, and some data at all

The next box on the form asks “How many tickets?”.

Think it through

List three different things a person might type, or fail to type, in the “How many tickets?” box that the program cannot use. Why can it not use each?

Show a model answer

For example: the word “two” (the program needs a number); nothing at all (the box was skipped, so there is no value); 2.5 (tickets are whole, so a number with a fractional part makes no sense); a negative number such as -3. Each one breaks a different rule.

Two more kinds of check deal with the first two of those mistakes:

  • A type check tests that the data is of the right data type. For tickets that means a whole number (an integer), so “two” and 2.5 are both refused.
  • A presence check tests that something has been entered at all. An empty box is refused, which matters for any item the program cannot work without, such as a name.

In pseudocode the presence check compares the input with an empty string: UNTIL Name <> "". A type check is usually left to the language: Python, for example, can test text with .isdigit(), which you will see in the Python box later. In an exam question you can describe the type check in words, such as “check that Tickets is an integer”.

Passing every validation check does not make data correct. Validation can only show that the data is possible: it follows the rules. Cara’s 15 passes the range check for Mia’s age, because 15 is between 11 and 18, even though Mia is 14. A program that validates is protected from impossible data, but not from a wrong value that looks reasonable.

Check your understanding

A parent submits the form with the surname box left empty. Which check refuses this?
Show the answer

Answer: D. A presence check, because nothing was entered.

A presence check asks only whether something was entered.

Check your understanding

Cara types 15 as Mia’s age, and the program accepts it. What does that tell you?
Show the answer

Answer: B. That 15 follows the rules for an age here, but may not be Mia’s real age.

Validation answers “is this allowed?”, never “is this true?”.

Stage: Discover

Data that must follow a pattern

The form asks for the date of the trip in the form 05/11/2026: two digits, a slash, two digits, a slash and four digits.

Think it through

Describe in words a rule a program could test to refuse 5/11/26 and 05-11-2026 but accept 05/11/2026. Which characters would it look at?

Show a model answer

A correct date has 10 characters, and the characters in positions 3 and 6 are slashes. 5/11/26 has only 7 characters, and 05-11-2026 has hyphens in positions 3 and 6, so both break the rule.

A format check tests that data follows a set pattern. The pattern for this date can be tested with the string routines from Lesson 8: the length must be 10, and SUBSTRING(Date, 3, 1) and SUBSTRING(Date, 6, 1) must both be "/".

IF LENGTH(Date) = 10 AND SUBSTRING(Date, 3, 1) = "/" AND SUBSTRING(Date, 6, 1) = "/"
  THEN
    OUTPUT "Date accepted"
  ELSE
    OUTPUT "Please type the date as 05/11/2026"
ENDIF

Work out what the check does with four typed dates:

Following the date format check
TypedLENGTHCharacter 3Character 6Accepted?
05/11/202610//Yes
5/11/2026
05-11-202610
31/02/202610//

    Show the completed table
    TypedLENGTHCharacter 3Character 6Accepted?
    05/11/202610//Yes
    5/11/2026912No
    05-11-202610--No
    31/02/202610//Yes

    31/02/2026 is accepted even though there is no 31st of February.

    Check your understanding

    Why does the format check accept 31/02/2026?
    Show the answer

    Answer: A. It checks the pattern of characters, not whether the date is real.

    Like every validation check, a format check can show that data has the right shape and no more.

    Stage: Explain

    Spotting a mistyped number: the check digit

    Riverside School’s library gives each member a card number of six digits. It is a fictional scheme, but real ones work the same way.

    Think it through

    A librarian types the card number 842134 instead of 482134 by swapping the first two digits. The typed number has six digits, all of them digits, and it is in the allowed range. How could the computer still notice that something is wrong?

    Show a model answer

    If the last digit is worked out from the other five by a fixed calculation, the computer can repeat the calculation on whatever has been typed and compare its answer with the last digit. A mistyped number will usually give a different answer, so the mismatch shows that something is wrong.

    A check digit is a digit added to a number, worked out from its other digits, so that a mistake can be noticed when the number is typed or scanned. The library’s rule (invented for this lesson, but the same idea as in ISBNs and bar codes) is:

    1. Multiply the first five digits by the weights 1, 3, 1, 3, 1, working from the left.
    2. Add the five products.
    3. Find the remainder when the total is divided by 10.
    4. The check digit is 10 minus that remainder. If the remainder is 0, the check digit is 0.
    1. Multiply each digit by its weight. 4 × 1 = 4, 8 × 3 = 24, 2 × 1 = 2, 1 × 3 = 3 and 3 × 1 = 3. (The weights alternate so that swapping two neighbouring digits usually changes the total.)
    2. Add the products. 4 + 24 + 2 + 3 + 3 = 36.
    3. Find the remainder when divided by 10. 36 leaves a remainder of 6.
    4. Subtract the remainder from 10. 10 − 6 = 4, so the check digit is 4 and the full card number is 482134.

    Now work out one yourself, and then check the number the librarian typed.

    The check digit for 31502
    DigitWeightDigit × weight
    313
    13
    51
    03
    21
    Total
    Remainder when divided by 10
    Check digit (10 − remainder)

      Show the completed table
      DigitWeightDigit × weight
      313
      133
      515
      030
      212
      Total13
      Remainder when divided by 103
      Check digit (10 − remainder)7
      Checking the typed number 842134
      StepWorkingResult
      Digits usedFirst five digits: 8, 4, 2, 1, 3
      Products8 × 1, 4 × 3, 2 × 1, 1 × 3, 3 × 1
      TotalAdd the products
      Check digit that the rule gives10 − (the remainder of 28 ÷ 10)
      Typed check digitThe sixth digit4
      VerdictDo the two check digits match?

        Show the completed table
        StepWorkingResult
        Digits usedFirst five digits: 8, 4, 2, 1, 3
        Products8 × 1, 4 × 3, 2 × 1, 1 × 3, 3 × 18, 12, 2, 3, 3
        TotalAdd the products28
        Check digit that the rule gives10 − (the remainder of 28 ÷ 10)2
        Typed check digitThe sixth digit4
        VerdictDo the two check digits match?No

        Check your understanding

        What does the program do with the typed number 842134, and why?
        Show the answer

        Answer: C. It rejects it: the check digit worked out is 2, but the typed one is 4.

        A check digit catches many typing mistakes, but not all: it shows that a number is probably typed correctly, not that it belongs to the right member.

        The Networks lesson Error Detection teaches the check digits used in ISBN-13 numbers and bar codes, including how they are checked. This lesson uses a smaller invented rule so that you can see how a check digit works as one kind of validation.

        Stage: Predict

        Is it what was meant?

        Ali types his email address into the booking form. It passes every validation check, but he has typed ali.kahn@school.example instead of ali.khan@school.example. Every character is allowed, so no validation rule can refuse it.

        Think it through

        Suggest two different ways of catching the swapped letters, one that a person does and one that the program does.

        Show a model answer

        A person could read the address back on the screen and compare it with the original they were copying from. The program could ask for the address twice and refuse it unless both entries are identical.

        Verification is a check that data has been entered as it was meant to be, rather than a check that it is allowed. Two methods are common:

        • A visual check is made by the person who typed. The program shows the data back, for example “You typed ali.kahn@school.example: is this correct?”, and the person compares it with the original.
        • A double entry check has the data typed twice. The program compares the two entries and accepts them only if they are identical.
        REPEAT
          OUTPUT "Enter your email address:"
          INPUT Email1
          OUTPUT "Enter it again:"
          INPUT Email2
          IF Email1 <> Email2
            THEN
              OUTPUT "The two entries do not match. Please try again."
          ENDIF
        UNTIL Email1 = Email2

        Do not confuse: Validation and verification

        What does it ask?Validation
        Is this data sensible and allowed by the rules?Age 140 is refused by a range check.
        What does it ask?Verification
        Is this the data that was meant, the same as the original or the second entry?ali.kahn is caught because it differs from the second entry.

        Neither can prove data is true. A program normally uses both, because each catches mistakes the other cannot.

        Check your understanding

        Ali types ali.kahn@school.example twice, in the same wrong way, and the double entry check passes. What does this show?
        Show the answer

        Answer: D. That a repeated mistake matches itself, so the check does not notice it.

        No single check catches every mistake.

        Stage: Explain

        Writing validation that asks again

        Think it through

        A program must keep asking for the age until a valid one is typed. Should the loop test the age before the first input or after it? Which of the three loops from Lesson 5 would you choose, and why?

        Show a model answer

        It must ask at least once before there is anything to test, so the test comes after the input: a post-condition loop, REPEAT … UNTIL. A pre-condition WHILE loop would need an extra INPUT before the loop to give it something to test.

        1. Choose the loop. The age must be asked for at least once, so REPEAT … UNTIL is used: it tests after the input.
        2. Ask and read. OUTPUT the prompt, then INPUT Age, inside the loop, so a refused age is asked for again.
        3. Give a helpful message. If the age breaks the rule, tell the person what is wrong and what is allowed. “Invalid” alone leaves them guessing.
        4. Write the loop condition as the opposite of the error. The loop must stop when the age is valid, so the UNTIL condition is the allowed range: Age >= 11 AND Age <= 18. The IF inside uses the refused range: Age < 11 OR Age > 18.
        REPEAT
          OUTPUT "Enter your age (11 to 18):"
          INPUT Age
          IF Age < 11 OR Age > 18
            THEN
              OUTPUT "Age must be from 11 to 18. Please try again."
          ENDIF
        UNTIL Age >= 11 AND Age <= 18

        Trace it for the three ages Ben types in turn: 9, then 25, then 14.

        Following the age validation loop
        AttemptAge typedMessage shown?Does the loop end?
        19
        225
        314

          Show the completed table
          AttemptAge typedMessage shown?Does the loop end?
          19YesNo
          225YesNo
          314NoYes

          Check your understanding

          Sam writes the loop condition UNTIL Age >= 11 OR Age <= 18. What does this version do?
          Show the answer

          Answer: B. It accepts every number, because every number is at least 11 or at most 18.

          For a range, the allowed condition joins its two parts with AND; the refused condition joins them with OR.

          Stage: Apply

          Write validation yourself

          Think it through

          Write pseudocode that asks for a password and keeps asking until its length is from 8 to 12 characters. Show a helpful message each time a password is refused.

          Show a model answer
          REPEAT
            OUTPUT "Choose a password (8 to 12 characters):"
            INPUT Password
            IF LENGTH(Password) < 8 OR LENGTH(Password) > 12
              THEN
                OUTPUT "The password must have from 8 to 12 characters. Please try again."
            ENDIF
          UNTIL LENGTH(Password) >= 8 AND LENGTH(Password) <= 12

          The IF and the UNTIL use opposite conditions. A presence check on its own would be UNTIL Password <> "", but this length check already refuses an empty password, because its length is 0.

          Go deeper The same ideas in Python
          def valid_age(text):
              return text.isdigit() and 11 <= int(text) <= 18
          
          age = input("Enter your age (11 to 18): ")
          while not valid_age(age):
              print("Age must be a whole number from 11 to 18. Please try again.")
              age = input("Enter your age (11 to 18): ")
          age = int(age)
          print("Age accepted:", age)
          • input always gives text, so the type check comes first: text.isdigit() is true only when the text is made of digits. Because and stops as soon as it knows the answer, int(text) is only tried on text that is made of digits.
          • Python’s loop that asks again is a while loop that tests first, so the first input is read before the loop and again at the end of each pass.
          • The check digit calculation is a loop over the digits, with int(digit) to turn each character into a number:
          def check_digit(first_five):
              total = 0
              for position, digit in enumerate(first_five):
                  weight = 1 if position % 2 == 0 else 3
                  total = total + int(digit) * weight
              return (10 - total % 10) % 10
          
          print(check_digit("48213"))
          print(check_digit("31502"))

          This prints 4 and then 7. The final % 10 makes the check digit 0, rather than 10, when the remainder is 0.

          Stage: Apply

          Choosing the check

          Apply it

          A school’s canteen app asks for the pupil’s year group, which must be from 7 to 11. Which check tests this?
          Show the answer

          Answer: C. A range check, because the value must lie between 7 and 11.

          Two limits mean a range check.

          Apply it

          Pupil numbers are always exactly six characters. Someone types 4821. Which check refuses it?
          Show the answer

          Answer: A. A length check, because it has four characters and six are needed.

          The wrong number of characters is exactly what a length check refuses.

          Apply it

          Which set of checks best protects the “How many tickets?” box, which should hold a whole number from 1 to 6?
          Show the answer

          Answer: B. A presence check, a type check and a range check.

          Several checks are often combined on one input.

          Stage: Consolidate

          Show what you know

          Think it through

          Explain the difference between validation and verification to someone who missed the lesson. Use the age on Mia’s form as your example, and say which of Ali’s, Ben’s and Cara’s mistakes each could catch.

          Show a model answer

          A good answer says that validation checks that data follows rules about what is allowed (a range check refuses Ali’s 140 and a type check refuses Ben’s “fourteen”), while verification checks that data is what was meant, by comparing it with the original or with a second entry. Cara’s 15 is allowed, so validation cannot refuse it. Only verification, such as a visual check against Mia’s details, could catch it.

          Apply it

          A new-account form asks the user to type their chosen password twice and refuses the form if the two differ. What kind of check is this?
          Show the answer

          Answer: D. A double entry check, a method of verification.

          Typing data twice to compare is verification.

          • Validation checks that data is sensible and allowed: range, length, type, presence, format and check digit. It cannot show that data is true.
          • Verification checks that data is what was meant: a visual check against the original, or a double entry check that compares two entries. A repeated mistake can still pass.
          • Validation in pseudocode is a REPEAT … UNTIL loop that asks again, with a helpful message. The IF uses the refused values and the UNTIL uses the allowed ones.
          • Use several checks together, because each catches mistakes that the others cannot.

          Sources and scope

          • What this lesson is. The twelfth lesson of the Algorithms and Programming module, written for this site. It covers point 7.5 of Cambridge IGCSE Computer Science 0478 (2026 to 2028, version 6) on validation and verification, with validation written in pseudocode. Its curriculum links are provisional and imply no review or endorsement by any examination board.
          • Original material. Riverside School, Mia, Ali, Ben, Cara, Sam, the trip form and the library card scheme are fictional, written for this lesson. The check digit rule is invented to show the idea; it is not the rule of any real scheme. The pseudocode follows the layout in the syllabus’s pseudocode guide, and each program’s output was worked out by hand.
          • Simplified on purpose. Only the checks listed in the syllabus’s notes and guidance are taught. Existence checks, limit checks and checks during data transfer (A Level) are left out. Type checking is shown in Python and in words, because the pseudocode guide has no routine for it.
          • Left to later lessons. Choosing test data for these checks (Lesson 13), finding errors in algorithms (Lesson 14) and writing a whole program for a scenario (Lesson 16). The Networks lesson Error Detection teaches the standard check digits of ISBNs and bar codes.