Validation
- Validation and verification are used to ensure input data is correct, reasonable and accurate
- Validation generally is about making sure data follows a set of specified rules created by the programmer. Verification is about double-checking input data to make sure it's what the user intended to enter
Validation
- Validation is the method of checking input data so that it is of an expected value and therefore accepted by the system
- Programmers create rules using code that automatically checks user input data to make sure the data is reasonable. If it is not reasonable then the data is rejected, usually with a message explaining why it was rejected and allowing the user to reenter the data
- The different types of validation rules or checks are as follows:
- Range checks
- Length checks
- Type checks
- Presence checks
- Format checks
- Check digits
- Each piece of data may require multiple different checks to be fully valid
Range check
- Range checks make sure the input data is a value between a user-defined minimum and maximum value, for example, a percentage being between 0 and 100 inclusive or a date in April is between 1 and 30 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
Length check
- Length checks check either that the input data is of an exact number of characters or in a user specified number range of characters
- A bank 4-digit PIN number may be an example of an exact number of characters. If it is not 4 digits in length it should be rejected
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
- Passwords usually have a specified range, for example, eight to twenty characters in length. If it does not fall within this range, it should be rejected
OUTPUT “Please enter a password between 8 and 20 characters”
REPEAT
INPUT Password
IF LENGTH(Password) < 8 OR LENGTH(Password) > 20
THEN
OUTPUT “Your password must be between 8 and 20 characters in length, please try again”
ENDIF
UNTIL LENGTH(Password) >= 8 AND LENGTH(Password) <= 20
Type checks
- Type checks make sure the input data is of the correct data type. For example, someone's age should be an integer (a whole number) whereas their name should be a string (a bunch of characters)
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)
Presence check
- Presence checks make sure that input data has been entered and that the input box has not been left empty
- A login system requires presence checks as both a username and password are required for authentication
OUTPUT “Enter your username”
REPEAT
INPUT Username
IF Username = “”
THEN
OUTPUT “No username entered, please try again”
ENDIF
UNTIL Username <> “”
Format check
- Format checks make sure that input data is of a predefined pattern
- Identification codes e.g. AP1234 and dates are examples of patterns
- Format checks are done using pattern matching and string handling
- The algorithm below checks a six digit identification number against the format “XX9999” where X is an uppercase alphabetical letter and 9999 is a four digit number
- 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
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
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
- Check digits help to identify errors in data entry such as mistyping, miscanning or misspeaking
- Such errors include missing or additional digits, swapped digits, mispronunciation of digits or simply an incorrect digit
Barcode ← “9780201379624”
Total ← 0
FOR Index = 1 to LENGTH(Barcode) - 1
IF Index MOD 2 = 0
THEN
Total ← Total + CAST_TO_INT(Barcode[Index])*3
ELSE
Total ← Total + CAST_TO_INT(Barcode[Index])*1
ENDIF
NEXT Index
CheckDigit ← 10 - Total MOD 10
IF CheckDigit = Barcode[LENGTH(Barcode)]
THEN
OUTPUT “Valid check digit”
ELSE
OUTPUT “Invalid check digit”
ENDIF