GoalDescribe presence, format, length and range checks, and say what validation can and cannot do.
New words
validation · an automatic check that data is reasonable before it is acceptedpresence check · checks that something has been entered in the fieldlength check · checks the number of charactersrange check · checks that a number is between a lowest and a highest value
Explanation
Validation rules help to keep data accurate. A presence check makes sure the field is not left empty. A format check makes sure that data follows a pattern, for example a date as DD/MM/YYYY. A length check makes sure the data has a sensible number of characters, such as exactly 5 for a code. A range check makes sure a number is between a lowest and a highest value. If the data fails a check, it is rejected and the user must try again. Validation checks that data is reasonable. It cannot prove that the data is true. A swimmer who types 15 instead of 13 passes a range check of 8 to 18, but the age is still wrong.
Check
What it does
Example rule
Presence
stops an empty field
name must not be empty
Format
makes data follow a pattern
code is two capital letters then three digits
Length
limits how many characters
code has exactly 5 characters
Range
keeps a number between two limits
age from 8 to 18
Worked examples
Python
1age = int(input("Age? "))
2if age >= 8and age <= 18:
3print("Accepted")
4else:
5print("Rejected")
Output
Accepted
A range check in Python. Ages from 8 to 18 are accepted. With 18 typed, the condition is true. With 7 or 19 it would be false, and the program would print Rejected.
Python
1name = input("Name? ")
2if name != "":
3print("Accepted")
4else:
5print("Please enter a name")
Output
Please enter a name
A presence check in Python. An empty answer is rejected. With nothing typed, name is an empty string, and name != "" is false.
Code typed
Format and length OK?
Reason
SW123
yes
two letters, then three digits, length 5
SW12
no
only two digits, length 4
sw123
no
the letters are not capitals
SWA23
no
a letter where a digit should be
SW1234
no
four digits, length 6
Format and length checks for a club code that must be two capital letters followed by three digits, so 5 characters in all.
Class activity
Rule cards (no computer). Write four fields on cards: first name, age (8 to 18), club code (two capital letters and three digits) and favourite number (1 to 10). Beside each, write a validation rule and its type. A partner reads out test data, for example 19, GH45 and an empty name. For each, you say accepted or rejected, and which check decided. Make three more tests that pass all the checks, even though the data might not be true. Explain what that shows about validation. If you have a computer or tablet, ask an adult to help you run the Python range check.
Practice
1
Name the type of check: (a) a rating must be between 1 and 10; (b) a password must have at least 8 characters; (c) the name box must not be empty; (d) a date must look like DD/MM/YYYY.
Rule
Type of check
(a) rating between 1 and 10
?
(b) password at least 8 characters
?
(c) name box must not be empty
?
(d) date looks like DD/MM/YYYY
?
(a) range check; (b) length check; (c) presence check; (d) format check.
2
A program accepts shoe sizes from 30 to 46. Which of these values are accepted: 30, 29, 46, 47, 38, 100?
30, 46 and 38 are accepted. 29, 47 and 100 are rejected.
3
A pupil aged 12 types 16, and the range check of 8 to 18 accepts it. Does this mean validation has failed? Explain.
No. Validation only checks that data is reasonable, and 16 is within the range. It cannot know that the age is not true. Other methods are needed to check the truth.
4
Which two checks would you put on a field for a 4-digit PIN-style number in an invented game? Say why.
A length check (exactly 4 characters) and a format check (digits only). Together they stop short entries and letters.