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:
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:
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:
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:
The code you need to perform a format check is shown below:
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: