Validation
What is validation?
• Validation is an automated process where a computer checks if a user input
is sensible and meets the program's requirements.
• There are six categories of validation which can be carried out
on fields and data types, these are
o Range check
o Length check
o Type check
o Presence check
o Format check
o Check digit
• There can be occasions where more than one type of validation will be
used on a field
• An example of this could be a password field which could have
a length, presence and type check on it
Range check
• Ensures the data entered as a number falls within a particular range
Pseudocode Ensures a percentage number is between 0-100 inclusive
OUTPUT “Enter a number between 0 and 100”
REPEAT
INPUT Number
IF Number < 0 OR Number > 100
THEN
OUTPUT “Number is not between 0 and 100, please try again”
ENDIF
UNTIL Number >= 0 AND Number <= 100
Python Ensures a user's age has been entered and falls between the digits of 0-110 inclusive
age = int(input("Enter your age"))
while age < 0 or age > 110:
age = int(input("Enter your age, ensure it is between 0-110"))
Length check
• Checks the length of a string
Pseudocode Ensures a pin number can only contain 4 digits
OUTPUT “Please enter your 4 digit bank PIN number”
REPEAT
INPUT Pin
IF LENGTH(Pin) <> 4
THEN
OUTPUT “Your pin number must be four characters in length, please try again”
ENDIF
UNTIL LENGTH(Pin) ← 4
Python Ensures a password is 8 characters or more
password_length = len(password)
while password_length < 8:
password = input("Enter a password which is 8 or more characters")
Type check
• Check the data type of a field
Pseudocode Ensures an age should be entered as an integer (whole number)
OUTPUT “Enter an integer number”
REPEAT
INPUT Number
IF Number <> DIV(Number, 1)
THEN
OUTPUT “Not a whole number, please try again”
ENDIF
UNTIL Number ← DIV(Number , 1)
Python Ensures an age should be entered as an integer (whole number)
age = input("Enter your age")
while [Link]() == False:
print("enter a number")
age = input("Enter your age as a number")
Presence check
• Looks to see if any data has been entered in a field
Pseudocode Ensures username and password are both entered
OUTPUT “Enter your username”
REPEAT
INPUT Username
IF Username = “”
THEN
OUTPUT “No username entered, please try again”
ENDIF
UNTIL Username <> “”
Python Ensures when registering for a website the name field is not left blank
name = input("Enter your name")
while name == "":
name = input("You must enter your name here")
Format check
• Ensures that the data has been entered in the correct format
• Format checks are done using pattern matching and string handling
Ensures a six digit ID number is entered against the format "XX9999" where X is an
Pseudocode
uppercase alphabetical letter and 9999 is a four digit number
INPUT IDNumber
IF LENGTH(IDNumber) <> 6
THEN
OUTPUT "ID number must be 6 characters long"
END IF
ValidChars ← "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
FirstChar ← SUBSTRING(IDNumber, 1, 1)
ValidChar ← False
Index ← 1
WHILE Index <= LENGTH(ValidChars) AND ValidChar = False DO
IF FirstChar = ValidChars[Index]
THEN
ValidChar ← True
ENDIF
Index ← Index + 1
ENDWHILE
IF ValidChar = False
THEN
OUTPUT "First character is not a valid uppercase alphabetical character"
ENDIF
SecondChar ← SUBSTRING(IDNumber, 2, 2)
ValidChar ← False
Index ← 1
WHILE Index <= LENGTH(ValidChars) AND ValidChar = False DO
IF SecondChar = ValidChars[Index]
THEN
ValidChar ← True
ENDIF
Index ← Index + 1
ENDWHILE
IF ValidChar = False
THEN
OUTPUT "Second character is not a valid uppercase alphabetical character"
ENDIF
Digits ← INT(SUBSTRING(IDNumber, 3, 6))
IF Digits < 0000 OR Digits > 9999
THEN
OUTPUT "Digits invalid. Enter four valid digits in the range 0000-9999"
ENDIF
Explanation
• The first two characters are checked against a list of approved characters
• The first character is compared one at a time to each valid character in the ValidChars array
• If it finds a match it stops looping and sets ValidChar to True
• The second character is then compared one at a time to each valid character in the ValidChars
array
• If it finds a match then it also stops looping and sets ValidChar to True
• Casting is used on the digits to turn the digit characters into numbers
• Once the digits are considered a proper integer they can be checked to see if they are in the
appropriate range of 0-9999
• If any of these checks fail then an appropriate message is output
Ensures an email contains a '@' and ' full stop (.) which follows the format
Python
"email@[Link]"
email = input("Enter your email address")
while "@" not in email or "." not in email:
email = input("Please enter a valid email address")
Check digits
• Check digits are numerical values that are the final digit of a larger code such
as a barcode or an International Standard Book Number (ISBN)
• They are calculated by applying an algorithm to the code and are then attached
to the overall code
Verification
What is verification?
• Verification is the act of checking data is accurate when entered into a system
• Mistakes such as creating a new account and entering a password incorrectly
mean being locked out of the account immediately
• Verification methods include:
o double entry checking
o visual checks
Double entry checking
• Double entry checking involves entering the data twice in separate input boxes
and then comparing the data to ensure they both match
• If they do not, an error message is shown
Pseudocode
REPEAT
OUTPUT “Enter your password”
INPUT Password
OUTPUT “Please confirm your password”
INPUT ConfirmPassword
IF Password <> ConfirmPassword
THEN
OUTPUT “Passwords do not match, please try again”
ENDIF
UNTIL Password ← ConfirmPassword
Visual checks
• Visual checks involve the user visually checking the data on the screen
• A popup or message then asks if the data is correct before proceeding
• If it isn’t the user then enters the data again
Pseudocode
REPEAT
OUTPUT “Enter your name”
INPUT Name
OUTPUT “Your name is: “, Name, “. Is this correct? (y/n)”
INPUT Answer
UNTIL Answer ← “y”