Combining validation checks

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

Combining validation checks

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. A password must be at least 8 characters AND not empty. Which condition combines both checks so the input must pass every one?
A LENGTH(Password) >= 8 AND Password <> ""
B LENGTH(Password) >= 8 OR Password <> ""
2. When two validation checks are joined with AND, when is the input accepted?
A When it passes at least one check
B Only when it passes both checks
3. Which condition checks that Age is a whole number between 11 and 16?
A Age >= 11 AND Age <= 16
B Age >= 11 OR Age <= 16
4. A username must be present and between 4 and 12 characters long. Which is a presence check plus a length check?
A Username <> "" AND LENGTH(Username) >= 4 AND LENGTH(Username) <= 12
B Username <> "" OR LENGTH(Username) >= 4 OR LENGTH(Username) <= 12
5. A student types 150 into a field that accepts marks from 0 to 100. The condition is: Mark >= 0 AND Mark <= 100. What happens?
A The condition is TRUE, because 150 is at least 0
B The condition is FALSE, so the input is rejected

Match the Pairs

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

Left columnRight column
1LENGTH(Code) = 6 AND Code <> "" ACombines two range limits so the value must be at or above 11 and at or below 16
2Age >= 11 AND Age <= 16 BCombines a length check and a presence check, so the input must pass both
3Show a separate error message for each check by testing the checks one at a time CIF Name = "" THEN OUTPUT "Enter a name" ELSE IF LENGTH(Name) > 10 THEN OUTPUT "Too long" ENDIF ENDIF
4All checks must be passed, so the valid condition joins them with AND DOR is used instead of AND, so almost every number passes the range check, because any number is either at least 11 or at most 16.
5IF Age >= 11 OR Age <= 16 THEN EThe THEN keyword is missing after the combined condition, so the IF statement is not written correctly.
6IF Code <> "" AND LENGTH(Code) = 6 OUTPUT "OK" FIF Mark >= 0 AND Mark <= 100 AND Mark = INT(Mark) THEN

Definitions

AND

Fill in the Blanks

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

1.
IF Name <> "" ___ LENGTH(Name) <= 20 THEN
2.
IF ______(Age) AND Age >= 12 THEN
3.
IF LENGTH(Code) __ 5 THEN OUTPUT "Code must be 5 characters" ENDIF
4.
REPEAT INPUT Age _____ Age >= 12 AND Age <= 18
5.
WHILE Age < 12 __ Age > 18 DO INPUT Age ENDWHILE

Spot the Error

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

Question 1
A INPUT Name
B Len ← LENGTH(Name)
C IF Name <> "" OR Len <= 10 THEN
D OUTPUT "Valid"
E ENDIF
Question 2
A INPUT Age
B IF Age >= 5 AND Age >= 9 THEN
C OUTPUT "Accepted"
D ENDIF
Question 3
A IF Num >= 1 AND <= 10 THEN
B OUTPUT "In range"
C ELSE
D OUTPUT "Out of range"
E ENDIF
Question 4
A INPUT Qty
B Ok ← IS_NUM(Qty) OR Qty > 0
C IF Ok = FALSE THEN
D OUTPUT "Invalid"
E ENDIF
Question 5
A IF Pin = "" THEN
B OUTPUT "PIN must be 4 long"
C ENDIF
D IF LENGTH(Pin) <> 4 THEN
E OUTPUT "PIN must be 4 long"
F ENDIF

Answer Key

Combining validation checks
Section A: Quick Questions
Q1
A — LENGTH(Password) >= 8 AND Password <> ""
Q2
B — Only when it passes both checks
Q3
A — Age >= 11 AND Age <= 16
Q4
A — Username <> "" AND LENGTH(Username) >= 4 AND LENGTH(Username) <= 12
Q5
B — The condition is FALSE, so the input is rejected
Section B: Match the Pairs
Q1
LENGTH(Code) = 6 AND Code <> "" → Combines a length check and a presence check, so the input must pass both
Q2
Age >= 11 AND Age <= 16 → Combines two range limits so the value must be at or above 11 and at or below 16
Q3
Show a separate error message for each check by testing the checks one at a time → IF Name = "" THEN OUTPUT "Enter a name" ELSE IF LENGTH(Name) > 10 THEN OUTPUT "Too long" ENDIF ENDIF
Q4
All checks must be passed, so the valid condition joins them with AND → IF Mark >= 0 AND Mark <= 100 AND Mark = INT(Mark) THEN
Q5
IF Age >= 11 OR Age <= 16 THEN → OR is used instead of AND, so almost every number passes the range check, because any number is either at least 11 or at most 16.
Q6
IF Code <> "" AND LENGTH(Code) = 6 OUTPUT "OK" → The THEN keyword is missing after the combined condition, so the IF statement is not written correctly.
Section C: Definitions
Q1
AND — Joins several validation checks into one condition, so the input must pass every check. Examples: presence and length together, or range and type together.
Section D: Fill in the Blanks
Q1
IF Name <> "" AND LENGTH(Name) <= 20 THEN
Q2
IF IS_NUM(Age) AND Age >= 12 THEN
Q3
IF LENGTH(Code) <> 5 THEN OUTPUT "Code must be 5 characters" ENDIF
Q4
REPEAT INPUT Age UNTIL Age >= 12 AND Age <= 18
Q5
WHILE Age < 12 OR Age > 18 DO INPUT Age ENDWHILE
Section E: Spot the Error
Q1
C — The presence and length checks must both pass, so they are joined with AND. With OR, a name that is too long would still be accepted as long as it is not empty. It should be IF Name <> "" AND Len <= 10 THEN.
Q2
B — The upper limit of the range check uses the wrong operator. Age >= 9 only accepts 9 or more, so the range 5 to 9 is not checked. It should be Age <= 9.
Q3
A — Each condition joined by AND must be complete and name the variable being checked. The second condition is missing Num. It should be IF Num >= 1 AND Num <= 10 THEN.
Q4
B — The type check and the range check must both pass, so they are joined with AND. OR would accept any value that passes just one of the checks. It should be Ok ← IS_NUM(Qty) AND Qty > 0.
Q5
B — Each check needs its own error message. The presence check should tell the user to enter a PIN, not give the length message. Line 2 should be OUTPUT "Enter a PIN".