Validation (Edexcel GCSE Computer Science): Revision Notes
Validation
What is validation?
Validation is a crucial programming concept that ensures the data your programme receives is suitable for processing. Think of it as a quality control system - it prevents poor quality or incorrect data (often called "garbage") from entering your programme. When bad data gets into a programme, it usually produces bad results, following the principle of "garbage in, garbage out."
The concept of "garbage in, garbage out" is fundamental in programming. No matter how well-written your code is, if you feed it bad data, you'll get unreliable results. This is why validation is considered one of the most important defensive programming techniques.
Programmers use several different types of validation checks to make sure user input meets the required standards before the programme processes it further.
Types of validation checks
Length check
A length check verifies that the user's input contains the correct number of characters. This is particularly useful when you need input to be a specific size, like a 4-digit PIN or a postcode.
Worked Example: Validating a 4-digit Key
key = "0000"
key = input("Enter key: ")
if len(key) != 4:
print("Invalid length")
else:
print("Good key")
In this example, the len() function counts the characters in the input, and we use the != operator to check if it's exactly 4 characters long.
The len() function counts the characters in the input, and you can use relational operators (like !=, ==, >, <) to check if it meets your requirements.
Presence check
A presence check ensures the user has actually entered something rather than leaving the input blank. This prevents your programme from trying to process empty data, which could cause errors.
Worked Example: Checking for Empty Input
key = "0000"
key = input("Enter key: ")
if len(key) == 0:
print("Must enter key")
if key == "":
print("Empty not allowed")
Both methods achieve the same result - they detect when no input has been provided.
An empty input is a common way to accidentally break a programme loop, so presence checks are essential for robust programmes. Always validate that users have actually provided input before processing it.
Range check
Range checks verify that a value falls between acceptable limits or boundaries. This works for both numbers and text characters, making it very versatile for different types of validation.
Worked Example: Validating Age and Letter Input
age = 0
letter = ""
age = int(input("Age: "))
if (age < 1) or (age > 110):
print("Invalid age")
letter = input("Letter: ")
letter = letter.upper()
if (letter >= "A") and (letter <= "Z"):
print("Good letter")
This example checks that age is between 1 and 110, and that the letter exists in the uppercase alphabet.
Range checks use relational operators (<, >, <=, >=) combined with Boolean operators (and, or) to test multiple conditions.
Combining validation techniques
In real programmes, you often need to use multiple validation checks together. For example, checking that input exists (presence), has the right length, and falls within acceptable bounds (range).
Best Practices for Effective Validation:
The key to effective validation is using:
- Relational operators to compare values
- Boolean operators to combine multiple conditions
- The len() function to check input size
- Appropriate error messages to guide users
Always combine different validation types for thorough data checking in real programmes.
Key Points to Remember:
- Validation prevents garbage data from entering your programme - always check user input before processing it
- Use len() function for length and presence checks - it counts characters and can detect empty inputs
- Range checks work with both numbers and text - use relational operators with Boolean logic to set boundaries
- Combine different validation types for thorough data checking in real programmes
- Provide clear error messages to help users understand what input is expected