Validation is the process of checking to see if data that is entered meets a set of requirements, this does mean it will always stop incorrect data being entered. For example if you had to enter a telephone number, you could validate it and say it needs to be 11 digits starting with a 0. The user could enter 01234567891, this meets the requirements set, but does not mean it is a valid telephone number.

There are some simple ways in which you can validate programs, these are:

  • Type Check - checking the data is of a particular type before continuing
  • Presence Check - checking that data is entered before continuing
  • Range Check - checking that data is between two boundaries set
  • Format Check - checking that data follows a set format

Type Check Example

In Python you can easily do a type check to check whether a value is a number or not a number, see the example below:

number = input("Enter a number ")
if number.isdigit() == True:
    print("You entered a number")
else:
    print("You didn't enter a number")

variable.isdigit() will return a value either True or False after checking to see if a variable is a number. You can then use an If to determine what to do.

When run in IDLE:

Presence Check Example

In Visual Basic you can use the len() function to find the length of a variable or form control. You can then check to if the length is 0 and if it is this means nothing has been entered, see the example below:

number = input("Please enter a number: ")
if len(number) == 0:
    print("Please enter a number")
else:
    print("Welcome to the program")

The len() function will return the length of the data stored in a variable. If the length is 0 this means nothing has been entered.

This is what happens when the button is clicked:

Range Check Example

You can also validate data by making sure it is within a certain range. For example, entering a test score you could say that it must be between 0 and 100. An example of how you could use a range check is shown below:

number = int(input("Please enter a number between 1 and 100: "))
if number > 0 and number <= 100:
    print("You entered a valid number")
else:
    print("You must enter a number between 1 and 100")

To perform a range check you can simply use an if and then use and to connect two conditions together.

This is what happens when the button is clicked:

Format Check Example

If you want to perform a format check in Python you will need to make use of a library. The Regular Expressions library allows you to set a pattern and then check to see if the data follows that pattern.

Below are some examples of Regular Expressions patterns as well as what they mean:

The program below will check if an email meets the right format requirements. For the purpose of this task the requirements are:

  • it must start with at least one alphanumeric character
  • followed by the @ sign
  • followed by at least one alphanumeric character
  • followed by a full stop (.)
  • followed by at least one alphanumeric character.

The regular expression pattern that is needed to do this is:

^[A-Za-z0-9]+\@[A-Za-z0-9]+\.[A-Za-z0-9]+$

The code you need to perform a format check is shown below:

import re
email = input("Enter an email address: ")
pattern = "^[A-Za-z0-9]+\@[A-Za-z0-9]+\.[A-Za-z0-9]+$"
emailMatch = re.match(pattern, email)
if emailMatch:
    print("This is a valid email format")
else:
    print("This is an invalid email format")
How it works
  • line 1 - imports the regular expressions library to be able to perform the format check
  • line 1 - declares a variable called email to store the email input by the user
  • line 2 - declares a variable called pattern to store the regular expression pattern that will be compared to the email address
  • line 3 - this is a function from the regular expressions library. It will see if the email address matches the pattern set. It will then store the result in the variable called emailMatch
  • line 4 - this will check if the emailMatch has been successful
  • line 5 - if it is it will say it is a valid email
  • line 7 - if it isn’t it will say it is an invalid email

This is what happens when the button is clicked:

The len() function will return the length of the data stored in a variable. If the length is 0 this means nothing has been entered.

This is what happens when the button is clicked: