Computer Science Essentials, Unit 3, Lesson 2 of 5

Validation and authentication

GCSE Computer Science, Writing code, 30 minutes

Sign in

Range, length, format and type checks written as functions, a loop that keeps asking until the input is valid, and a login routine with a limited number of attempts.

What you will learn

  • Write range, length, format and type checks
  • Write a loop that re-asks until the input is valid
  • Write a login routine with a limited number of attempts
  • Choose normal, boundary and erroneous test data

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 <=.

A length check uses len(): a code must be from 4 to 9 characters long when len(code) >= 4 and len(code) <= 9.

A format check looks at each character. Characters compare by their codes, so ch >= "A" and ch <= "Z" is True for a capital letter and ch >= "0" and ch <= "9" is True for a digit.

Python also has ch.isupper(), ch.islower() and ch.isdigit(), which test one character. Check the length first: reading code[3] from a code that is only 2 characters long stops the program with an error.

Two capital letters then one digit, such as KT7

function valid_code(code)
    if code.length != 3 then
        return false
    endif
    for i = 0 to 1
        if code.substring(i, 1) < "A" OR code.substring(i, 1) > "Z" then
            return false
        endif
    next i
    if code.substring(2, 1) < "0" OR code.substring(2, 1) > "9" then
        return false
    endif
    return true
endfunction
print(valid_code("KT7"))
print(valid_code("K77"))
SUBROUTINE valid_code(code)
    IF LEN(code) ≠ 3 THEN
        RETURN False
    ENDIF
    FOR i ← 0 TO 1
        IF SUBSTRING(i, i, code) < "A" OR SUBSTRING(i, i, code) > "Z" THEN
            RETURN False
        ENDIF
    ENDFOR
    IF SUBSTRING(2, 2, code) < "0" OR SUBSTRING(2, 2, code) > "9" THEN
        RETURN False
    ENDIF
    RETURN True
ENDSUBROUTINE
OUTPUT valid_code("KT7")
OUTPUT valid_code("K77")
def valid_code(code):
    if len(code) != 3:
        return False
    for i in range(2):
        if code[i:i + 1] < "A" or code[i:i + 1] > "Z":
            return False
    if code[2:3] < "0" or code[2:3] > "9":
        return False
    return True
print(valid_code("KT7"))
print(valid_code("K77"))

This prints True, then False: in "K77" the second character is not a capital letter.

In Python, text[1:] is everything from position 1 to the end, so it drops the first character. A type check on typed text can use this: if the text starts with "-", drop it, then check that what is left is not empty and that every character is a digit.

The rest of this lesson and its 9 practice questions are locked.

In this lesson

  1. Validation checks
  2. Keep asking until it is valid
  3. Authentication
  4. Try it: A range check

9 practice questions, marked as you go; about 30 minutes in all.

Key terms

Validation, Authentication, Flag, Parallel lists

In this unit: Writing code

Write real Python that runs against tests: functions, validation and logins, strings, lists and 2D lists, records, and completing and refining a program.

All of Writing code