Validation

Input Validation

A data set is no use if it contains incorrect data. As far as possible, it is best to avoid inputting invalid data in the first place. Most mistakes can be prevented by setting the software to reject certain values that are unacceptable. This happens at the time of input.

Data validation aims to make sure that data is sensible, reasonable, complete and within acceptable boundaries.

For example, a secondary school student is likely to be aged between 11 and 16. The computer can be programmed only to accept numbers between 11 and 16. This is a range check.

However, this does not guarantee that the number typed in is correct. For example, a student's age might be 14, but if 11 is entered it will be valid but incorrect.

Typical Validation Checks:

Example of Validation

#Program to capture user name and check name is between 2 and 20 characters
name = input("Please enter name: ")
while (name.length < 2) OR (name.length> 20)
	print("Must be between 2 and 20 characters - please re-enter: ")
	name = input("Please enter name: ")
endwhile


Example 2


#How to make sure the user enters a number (integer)

def inputNumber(message):
  while True:
    try:
       userInput = int(input(message))       
    except ValueError:
       print("Not an integer! Try again.")
       continue
    else:
       return userInput 
       break 
     

#MAIN PROGRAM STARTS HERE:
age = inputNumber("How old are you?")

if (age>=18):
  print("You are old enough to vote.")
else:
  print("You will be able to vote in " + str(18-age) + " year(s).")


There are two main methods of verification:

Summary

Validation and verification are two ways to check that the data entered into a computer is correct. Data entered incorrectly is of little use.