Open the app
  1. Home
  2. Lessons
  3. GCSE & IGCSE Computer Science
  4. Validation and authentication

Validation and authentication

🎬 The doodle video for this lesson is coming soon. Subscribe on YouTube to see it first.

Users make mistakes and some try to break things. Validation checks data is sensible; authentication checks who someone is.

Validation checks

Validation with a loop

Keep asking until the data is valid:
age = int(input("Age: "))
while age < 11 or age > 18:
    print("Out of range")
    age = int(input("Age: "))
print("Accepted")

Authentication

Authentication confirms that users are who they say they are.
The usual method is an ID and password.
A lookup checks the entered details against stored records, for example a list or a file of users.
users = [["amir", "Cat#42"], ["beth", "Sun!77"]]
uid = input("ID: ")
pw = input("Password: ")
found = False
for user in users:
    if user[0] == uid and user[1] == pw:
        found = True
print(found)
Worked example

Which validation checks suit a UK mobile number entered as 11 digits starting 07?

  1. It must be entered: presence check.
  2. It must be 11 characters: length check.
  3. It must start with 07 and contain only digits: pattern check.

Answer: Presence, length and pattern checks

Key idea

Presence, length, range and pattern checks make sure data is sensible. A while loop re-asks until valid. Authentication confirms identity, usually by looking up an ID and password.

The interactive lesson includes the diagrams for this topic.

Check you have got it

Answer 7 quick questions with instant marking. If you get one wrong, GCSE-ready shows you why and gives you another go. It is free, and you do not need an account.