Revise Computingrevisecomputing.co.uk
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates
At a glanceFeaturesStudentsPricingHow it worksFree GCSE notesExam dates

Knowledge organisers / Defensive Design

Input validation

All topicsPractise exam questions
Knowledge organiser

Defensive Design

2.3.1b

Practical experience of designing input validation and simple authentication (e.g. username and password)

What you need to know

Input validation checks that data entered by a user is reasonable and appropriate before the program processes it. Common types include presence checks (data was entered), range checks (value falls within limits), length checks (correct number of characters), type checks (correct data type), and format checks (correct pattern).

Key points

  • Definition:Range check: ensures a value falls within an acceptable range, e.g. nights >= 1 AND nights <= 5.
  • Definition:Presence check: ensures data has been entered and is not blank (e.g. firstname != "").
  • Definition:Length check: ensures the input has the correct number of characters, e.g. code.length == 3.
  • Definition:Type check: a validation check that ensures data is of the correct data type (e.g. integer, string).
  • Definition:Format check: a validation check that ensures data follows a specific pattern or structure (e.g. DD/MM/YYYY).
  • Definition:Look-up check: a validation check that compares input data against a predefined list of valid values.
  • Validation does NOT check if data is CORRECT, only that it is reasonable and in the right format.
  • Validation is usually implemented using loops (while) that keep asking until valid data is entered.
  • Exam Tip:When checking MULTIPLE validations, all must pass for ALLOWED and any failure gives NOT ALLOWED. Use a Boolean flag (e.g. valid = True) and set to False if any check fails.
  • Exam Tip:In range checks, you must compare the variable in BOTH parts: if h >= 40 AND h <= 180. NOT: if h >= 40 AND <= 180 (missing variable reference).
  • Exam Tip:Use >= and <= in code, NOT the maths symbols ≥ or ≤ (not available on a keyboard). Also =< is WRONG — must be <=.
  • Common Mistake:if room == "basic" or "premium" is WRONG. You must compare the variable each time: if room == "basic" or room == "premium".
  • Common Mistake:if room != "basic" or room != "premium" is ALWAYS True (any value will fail one test). Use AND: if room != "basic" AND room != "premium".
  • Exam Tip:Name the validation type AND explain HOW it applies to the scenario. E.g. 'Range check — make sure the player's answer is between 2 and 20 inclusive'.
  • Exam Tip:Validation only applies to USER inputs. Do not validate randomly generated values.

Code examples

Input validation examples
python
# Range check
num = int(input("Enter a number 1-10: "))
while num < 1 or num > 10:
    print("Out of range!")
    num = int(input("Enter a number 1-10: "))

# Presence check
name = input("Enter your name: ")
while name == "":
    name = input("Name cannot be blank: ")

# Length check
password = input("Enter password (min 8 chars): ")
while len(password) < 8:
    password = input("Too short! Try again: ")