Choosing a validation check for a field

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Choosing a validation check for a field

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A form asks for a student's age, which must be between 11 and 18. Which validation check is the best choice?
A Presence check
B Range check
2. A 4-digit PIN is being entered. Which pair of checks suits this field?
A Length check and type check
B Range check and format check
3. A user must not leave the Name field blank. Which check should you choose?
A Check digit
B Presence check
4. A postcode must follow a set pattern of letters and numbers, such as 'AB12 3CD'. Which check is best?
A Format check
B Range check
5. A shop scans ISBN numbers on books. Which check can detect a mistyped digit?
A Check digit
B Presence check

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1Age: range check (0 to 120) ARecalculate the check digit and compare it
2Name: presence check BLENGTH(PIN) = 4 AND PIN is all digits
3A PIN must be exactly 4 digits, so a length check and a type check are both needed CA postcode is a pattern of letters and digits, so it needs a format check, not a range check.
4An ISBN or barcode can be checked for typing errors using its last digit, so a check digit is chosen DA presence check only tests that something was entered, so 500 would still pass. Use a range check for age.
5Postcode field: use a range check (1 to 9999) EAge is a number that must sit between a lowest and highest sensible value, so a range check is the best choice.
6Age field: use a presence check to make sure age is sensible FA name must not be left empty, so a presence check is chosen to make sure something has been entered.

Definitions

validation check

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
// Age must be between 0 and 120 IF Age >= 0 ___ Age <= 120 THEN OUTPUT "Age accepted"
2.
// PIN must have exactly 4 digits IF ______(PIN) <> 4 THEN OUTPUT "PIN must be 4 characters"
3.
INPUT Name IF LENGTH(Name) = _ THEN OUTPUT "Name is required"
4.
// Date of birth needs a ______ check
5.
// ISBN: validate using a ___________

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A // Age field: range check
B INPUT Age
C WHILE Age < 0 AND Age > 120 DO
D OUTPUT "Enter 0 to 120"
E INPUT Age
F ENDWHILE
Question 2
A INPUT PIN
B WHILE PIN <> 4 DO
C OUTPUT "PIN must be 4 chars"
D INPUT PIN
E ENDWHILE
Question 3
A INPUT Name
B WHILE Name <> "" DO
C OUTPUT "Name is required"
D INPUT Name
E ENDWHILE
Question 4
A CASE OF Field
B "Age": Check ← "Range"
C "Name": Check ← "Presence"
D "Date": Check ← "Range"
E ENDCASE
Question 5
A Field ← "ISBN"
B Check ← "Presence check"
C Reason ← "Detects typos"
D OUTPUT Check, Reason

Answer Key

Choosing a validation check for a field
Section A: Quick Questions
Q1
B — Range check
Q2
A — Length check and type check
Q3
B — Presence check
Q4
A — Format check
Q5
A — Check digit
Section B: Match the Pairs
Q1
Age: range check (0 to 120) → Age is a number that must sit between a lowest and highest sensible value, so a range check is the best choice.
Q2
Name: presence check → A name must not be left empty, so a presence check is chosen to make sure something has been entered.
Q3
A PIN must be exactly 4 digits, so a length check and a type check are both needed → LENGTH(PIN) = 4 AND PIN is all digits
Q4
An ISBN or barcode can be checked for typing errors using its last digit, so a check digit is chosen → Recalculate the check digit and compare it
Q5
Postcode field: use a range check (1 to 9999) → A postcode is a pattern of letters and digits, so it needs a format check, not a range check.
Q6
Age field: use a presence check to make sure age is sensible → A presence check only tests that something was entered, so 500 would still pass. Use a range check for age.
Section C: Definitions
Q1
validation check — A test matched to the field: range for age, length and type for PIN, presence for name, format for dates or postcodes, check digit for ISBN. Always justify the choice.
Section D: Fill in the Blanks
Q1
// Age must be between 0 and 120 IF Age >= 0 AND Age <= 120 THEN OUTPUT "Age accepted"
Q2
// PIN must have exactly 4 digits IF LENGTH(PIN) <> 4 THEN OUTPUT "PIN must be 4 characters"
Q3
INPUT Name IF LENGTH(Name) = 0 THEN OUTPUT "Name is required"
Q4
// Date of birth needs a format check
Q5
// ISBN: validate using a check digit
Section E: Spot the Error
Q1
C — A range check on age rejects a value that is too low OR too high. No number is both below 0 and above 120, so AND never rejects anything. It should be OR.
Q2
B — A length check must count the characters with LENGTH(PIN). Comparing PIN itself with 4 tests the value, not how many characters it has. It should be WHILE LENGTH(PIN) <> 4 DO.
Q3
B — A presence check must repeat the question when the field is empty. Name <> "" repeats when something has been entered, which is backwards. It should be WHILE Name = "" DO.
Q4
D — A date has a fixed pattern such as DD/MM/YYYY, so a format check is the best fit. A range check suits numbers like age. The line should assign "Format".
Q5
B — An ISBN needs a check digit, which is calculated from the other digits and detects mistyped numbers. A presence check only tests that something was entered. It should be "Check digit".