Validation
CT04c
-
Validate input using presence check, length check, range check, and look-up check.
Interactive Programs
CT04c
Previously you have learned how to make your programs interactive by getting the user to enter information.
You did this using the input() function.
This function returns a string containing what the user types in.
You then use type conversion functions to change data types, so that you could perform a range of operations on the input data.
When you did this, you encountered errors if the user typed in something the program did not expect.
Unexpected input
CT04c
Unexpected input is a common problem in programming.
Programmers need to design programs so they do not crash if they receive data that is not of an expected type.
This is where validation comes in.
Validation is the process of checking that when data is entered it is:
- suitable for processing
- in an expected format
- within a suitable range
Be warned, just because data is valid, it doesn’t necessarily mean it is correct!
Validation
CT04c
All programs that take input of any kind (not just when a user types something in) should have validation capabilities to handle unexpected input.
This makes programs more user friendly as error messages can be output that highlight any problem to users without the program crashing.
There are four kinds of check that can be carried out:
- range check: does the input fall within a specific range of values?
- length check: is the input the expected length?
- presence check: has anything actually been entered?
- look-up check: is the input one from a given list of possible options?
Range check
CT04c
Range checks are used to ensure that input falls within a given range of expected inputs.
They are useful for numerical data entry, but characters can also be checked in this way.
Range check examples
CT04c
Dates – e.g. must not be in the future or too far in the past, depending on the application.
Characters – e.g. passwords must contain a symbol / capital / number, etc.
Temperature – e.g. must not go below absolute zero (-273 degrees C).
Height – e.g. must not be greater than that of the tallest human on record (2.72m).
Age – e.g. a limit on signing up for an account. Note that age can always be calculated given a birthdate and the current date.
Length check
CT04c
Length checks are used to check the number of characters that have been entered.
Examples include:
- passwords must be at least 8 characters long
- username length limits
- tweets (280 characters maximum).
Presence check
CT04c
This is the type of validation you might be most familiar with.
Presence checks are used to ensure that something has been entered.
They are commonly highlighted by an asterisk on a form.
When input is required in a field, it is referred to as a required field.
Last Name:
First Name:
*required
*required
Look-up checks
CT04c
This kind of validation involves comparing the input to a list of acceptable values.
If the input doesn’t match an item in the list, it is considered invalid and rejected.
Look-ups use lists of acceptable inputs, these are known as whitelists.
Menus are a kind of look-up check. When you program a menu system, you should include code that only accepts inputs that match the menu options.
Activity 1
CT04c
Menus are a kind of look-up check. When you program a menu system, you should include code that only accepts inputs that match the menu options.
Description | Type of check required | Valid input | Invalid input |
---|---|---|---|
A phone number must be given. | Presence check | 01234567890 | <empty string> |
January = 1, February = 2, … December = 12. | Range check | 7 | 22 |
A phone number must be 11 characters long. | Length check | 01234567890 | 012345 |
Personal Identification Numbers (PIN) must be 4 digits. | Range check (0001-9999) | ||
Length check | 2468 | 12 | |
Email addresses can only contain alphabetic characters, digits 1 to 9, and the symbols . and @. | Lookup check | me@where.com | me@some$$.com |
A login name must not be blank. | Presence check | ||
Length check | student123 | <empty string> | |
A noughts and crosses game uses the symbols ‘X’ and ‘O’. | Lookup check | X | Q |
A computer program uses a menu system with choices A, B, C, and Q. | Lookup check | A | 3 |
In Python
CT04c
You should use selection to perform validation – if the check is met, the input is valid, otherwise it is invalid.
To make programs useful, validation should be done in a loop so that the program will keep asking the user to enter a value until they enter a valid one.
To make programs user-friendly, validation should include messages to inform the user whether the input has been accepted or not and why.
In Python
CT04c
This simple program asks the user for three pieces of information and performs three checks
# Checking age is greater than 11
userAge = int(input("Enter your age: "))
if userAge > 11:
print("Age valid")
else:
print("Invalid. You must be older than 11")
# Checking name is at least 3 characters long
userName = input("Enter your name: ")
if len(userName) >= 3:
print("Name valid")
else:
print("Invalid. Name must be at least 3 characters")
# Checking that a password has been entered
password = input("Enter a password: ")
if password != "":
print("Password accepted")
else:
print("You must enter a password")
You can see that each check is a single if statement.
This program is incomplete – loops need to be added to ensure that the user must enter valid input.
In Python
CT04c
This version is much more useful.
# Checking age is greater than 11
userAge = int(input("Enter your age: "))
while userAge <= 11:
print("Invalid. You must be older than 11")
userAge = int(input("Enter your age: "))
print("Age valid")
# Checking name is at least 3 characters long
userName = input("Enter your name: ")
while len(userName) < 3:
print("Invalid. Name must be at least 3 characters")
userName = input("Enter your name: ")
print("Username valid")
# Checking that a password has been entered
password = input("Enter a password: ")
if password == "":
password = input("Enter a password: ")
print("You must enter a password")
print("Password accepted")
Each check is built into a while loop.
The loop will run until the user enters a value that is valid.
When a valid input is given, that loop ends and the valid message is output before the next check is done.
Look-up checks
CT04c
These can be carried out using lists in Python.
menuOptions = ["1", "2", "9"]
menuText = ("Choose an option from the menu:" +
"\n1. Play game" +
"\n2. View highscores" +
"\n9. Exit")
choice = ""
print(menuText)
userChoice = input("Choice: ")
while userChoice not in menuOptions:
print("Please choose an option from the menu.")
print(menuText)
userChoice = input("Choice: ")
If there are only a few options to choose from (a menu for example) this can be done in a single instruction.
Review
CT04c
Validate input using presence check, length check, range check, and look-up check.
- Presence checks that a value has been entered, it is used for required fields.
- Length checks the number of characters in an input.
- Range checks the value of an input against a specific range of acceptable values.
- Look-up checks an input against a list of specific acceptable inputs (a whitelist).
ct04c Validation
By David James
ct04c Validation
- 420