Unit 3, Lesson 2 of 5

Validation and authentication

About 3 min to read, then 9 practice questions

Validation checks

Validation checks that input is sensible before a program uses it. It cannot check that the data is true: a valid age can still be the wrong age.

  • Range check: a number is between two limits, such as a month from 1 to 12.
  • Length check: text has an allowed number of characters.
  • Presence check: something has been entered at all.
  • Format check: text follows a pattern, such as two letters then three digits.
  • Type check: the input is the right kind of data, such as a whole number.

A range check as a function

function valid_month(month)
    return month >= 1 AND month <= 12
endfunction
print(valid_month(12))
print(valid_month(13))
SUBROUTINE valid_month(month)
    RETURN month ≥ 1 AND month ≤ 12
ENDSUBROUTINE
OUTPUT valid_month(12)
OUTPUT valid_month(13)
def valid_month(month):
    return month >= 1 and month <= 12
print(valid_month(12))
print(valid_month(13))

This prints True, then False. Both limits are allowed, so the comparisons are >= and <=.

Keep asking until it is valid

A validation loop reads the input, then repeats while the input is not acceptable. The first read is before the loop, so the condition has something to check.

Re-ask until the number is from 1 to 10

n = int(input())
while n < 1 OR n > 10
    print("Out of range")
    n = int(input())
endwhile
print("Accepted")
n ← STRING_TO_INT(USERINPUT)
WHILE n < 1 OR n > 10
    OUTPUT "Out of range"
    n ← STRING_TO_INT(USERINPUT)
ENDWHILE
OUTPUT "Accepted"
n = int(input())
while n < 1 or n > 10:
    print("Out of range")
    n = int(input())
print("Accepted")

The examples here call input() with no prompt message. In the questions, do the same, so the output is only the lines the task asks for.

Test a check with three kinds of data:

  1. Normal data: a typical value that should be accepted, such as 5.
  2. Boundary data: the values on each limit and just outside them, such as 0, 1, 10 and 11.
  3. Erroneous data: values that should be rejected, such as 25.

Authentication

Authentication checks that a user is who they say they are, usually with a username and a password. A good routine limits the number of attempts, so a password cannot be guessed by trying again and again.

Three attempts at a password

attempts = 0
ok = false
while attempts < 3 AND NOT ok
    word = input()
    if word == "Tiger42" then
        ok = true
    else
        print("Incorrect")
        attempts = attempts + 1
    endif
endwhile
if ok then
    print("Welcome")
else
    print("Locked")
endif
attempts ← 0
ok ← False
WHILE attempts < 3 AND NOT ok
    word ← USERINPUT
    IF word = "Tiger42" THEN
        ok ← True
    ELSE
        OUTPUT "Incorrect"
        attempts ← attempts + 1
    ENDIF
ENDWHILE
IF ok THEN
    OUTPUT "Welcome"
ELSE
    OUTPUT "Locked"
ENDIF
attempts = 0
ok = False
while attempts < 3 and not ok:
    word = input()
    if word == "Tiger42":
        ok = True
    else:
        print("Incorrect")
        attempts = attempts + 1
if ok:
    print("Welcome")
else:
    print("Locked")

The counter limits the attempts and the flag stops the loop as soon as the password is right. When both a username and a password are needed, both must match: join the two comparisons with AND.

Practise: While loops and Testing on Brainlag Code.

Practise this: 9 questions, about 17 min

Answering from memory soon after reading is what makes it stick, so now is a good time.

  • XP for every question right first time
  • This lesson fills in on your course map
  • Your daily streak, kept going
Start practising