Programming · Validation
up to +100 XP
Stop 2.8 · Programming · 3 min

Validation

Ensure data integrity and prevent errors

Learning objectives:
  • Define validation and know when each check is used (2.8.1)
  • Interpret, develop and debug code using validation (2.8.2–2.8.4)

What validation is

Validation is an automatic check that input data is sensible and reasonable before it is accepted. It cannot prove the data is correct: a valid date of birth can still be the wrong one.

The six validation checks

CheckWhat it ensuresExample
PresenceA value has been entered, not left blankEmail cannot be empty
LengthThe data has the right number of charactersPassword at least 8 characters
RangeA number is between a minimum and maximumAge 16 to 19
TypeThe data is the right typeQuantity must be a whole number
FormatThe data follows a patternA postcode or an email address
Check digitA code is valid, using an extra digit calculated from the othersAccount numbers, barcodes

You do not need to know a particular check digit algorithm, only what it is for.

Validation in Python

name = input("Name: ")
if name == "":                                  # presence
    print("Please enter your name.")

password = input("Password: ")
if len(password) < 8:                           # length
    print("Password must be at least 8 characters.")

age_text = input("Age: ")
if not age_text.isdigit():                      # type
    print("Age must be a whole number.")
elif not 16 <= int(age_text) <= 19:             # range
    print("Age must be between 16 and 19.")

code = input("Class code (e.g. AB123): ")
if not (len(code) == 5 and code[:2].isalpha() and code[2:].isdigit()):  # format
    print("Use two letters then three numbers.")

Combine checks where needed, and repeat the question with a while loop until the data is valid.

Common validation bugs

Quick check · 11 questions · up to 100 XP

Check what stuck.

+10 XP each
Question 1

Question 1 of 11

What does validation check?

Question 2

Question 2 of 11

Which check makes sure a field has not been left blank?

Question 3

Question 3 of 11

Which check makes sure an age is between 16 and 19?

Question 4

Question 4 of 11

Which check makes sure a password has at least 8 characters?

Question 5

Question 5 of 11

Which check makes sure a postcode follows a pattern?

Question 6

Question 6 of 11

Which check uses an extra digit calculated from the other digits?

Question 7

Question 7 of 11

Which Python code is a type check that the input is a whole number?

Question 8

Question 8 of 11

What is wrong with if age > 16 and age < 19 for "16 to 19 inclusive"?

Question 9

Question 9 of 11

Why validate before using int() on input?

Question 10

Question 10 of 11 · Match the pairs

Match each validation check to an example.

Range check
Presence check
Length check
Format check
Question 11

Question 11 of 11 · Coding

Write valid_age(age) that returns True only if age is between 16 and 19 inclusive.

Open the IDE
Hint

Python allows 16 <= age <= 19.