Input Validation Flashcards

All 9 cards in this deck

What is input validation?

A check that only sensible/allowed data is entered into a program, e.g. restricting a rating to 1 to 10.

What does a range check do?
e.g. a height in cm must be from 40 to 180

Checks the value entered lies between a stated lower and upper limit — here, that the height is at least 40 and no more than 180.

What does a presence check do?

Checks that data has actually been entered and the field has not been left blank.

What does a length check do?

Checks that the data entered has an allowed number of characters, e.g. a password of at least 8 characters.

What does a type check do?

Checks that the data entered is of the correct data type, e.g. an integer for an age.

What does a format check do?

Checks that the data entered follows a set pattern, e.g. a postcode or a date as dd/mm/yyyy.

What should a program do when it detects invalid data?

Reject the data, output a helpful error message and ask the user to enter it again, rather than crashing or storing it.

What are the steps to write an algorithm that validates an input and re-prompts until it is valid?
e.g. a height in cm must be from 40 to 180

  1. Input the value into a variable: height = input("Enter height")
  2. Loop while the value is outside the range: while height < 40 OR height > 180
  3. Inside the loop, output an error message and input the value again; the loop ends when the value is valid.

True or false? if height >= 40 AND <= 180 is an acceptable way to write a range check.

False. The variable must be repeated in each condition: if height >= 40 AND height <= 180.