Learn
7.5.2 | FORMAT CHECKS AND CHECK DIGITS
01 | THE RIGHT LENGTH IS NOT THE WHOLE STORY
A school identifier must contain two uppercase letters followed by four digits. AB1234 looks suitable. So does XY0042. But 1234AB has the same six-character length and still breaks the rule.
A format check checks whether data follows a specified pattern or structure. It examines which kinds of characters appear and where they appear.
The previous lesson’s length check counts characters. A format check can go further: exactly two letters first, then exactly four digits. A complete pattern rule often also implies a particular length.
Neither check proves that the identifier belongs to a real student. They test the representation, not every fact the code is meant to identify.
02 | DEFINE THE PATTERN BEFORE CHECKING IT
“Check the format” is incomplete unless the required format is known. For this example, the rule is: exactly six characters, positions 1 and 2 are uppercase English letters A to Z, and positions 3 to 6 are digits 0 to 9.
1
ALETTER
2
BLETTER
3
1DIGIT
4
2DIGIT
5
3DIGIT
6
4DIGIT
The zeros in XY0042 are part of the identifier’s text. It should not be converted into a quantity that loses those characters.
Make any permitted alternatives explicit. Our example rejects lowercase letters. A different system might accept them or convert them to uppercase before checking, but that would be a different specified rule.
A precise rule lets a programmer construct a check and a tester predict which entries should pass.
03 | APPLY THE SAME RULE TO DIFFERENT INPUTS
Follow the rule position by position. Check length first so that you do not try to inspect a character position that does not exist.
| Input | Format outcome | Reason |
|---|---|---|
| AB1234 | Pass | Two uppercase letters then four digits |
| XY0042 | Pass | Leading zeros allowed in the digit positions |
| 1234AB | Fail | Wrong positions |
| ab1234 | Fail | Lowercase not permitted by this rule |
| AB12C4 | Fail | Letter in a digit position |
| AB123 | Fail | Wrong length |
AB12C4 fails because position 5 contains a letter where a digit is required. AB123 fails because it is too short. A six-character code can fail even though every character belongs to one of the permitted categories overall: their positions also matter.
The code AB9999 passes the pattern rule. Whether it is a registered student code requires a separate lookup or other rule. Passing a format check does not establish that a code exists.
04 | PATTERN CHECKS HAVE CLEAR LIMITS
A date written as 31/02/2026 has the visible pattern two digits, slash, two digits, slash, four digits. That does not make it a real calendar date. February does not have thirty-one days.
A format check can test the arrangement, while additional checks establish valid calendar values and relationships. Similarly, an email-like string with a permitted structure does not prove that a mailbox exists or that a message can be delivered.
A typed code might follow the right pattern but identify the wrong person. An entered date might be valid yet be the wrong date for the event.
Keep the claim precise: a format check establishes conformance to the pattern it tests. It does not establish all possible validity rules or factual accuracy.
05 | HOW TO DESIGN A FORMAT CHECK
For the school code, break the check into manageable decisions: is the length six, are the first two characters allowed letters, and are the last four allowed digits?
Once a required condition fails, the code can be rejected with a useful message. If every required condition succeeds, it passes this format rule.
You can describe those decisions precisely without inventing a Cambridge keyword called FORMATCHECK. A real implementation might inspect characters individually or use a pattern-matching facility supplied by its programming language.
In this lesson, the format design is expressed as stated tests rather than a claim that an undefined helper routine will run. When given specific pseudocode tools in a question, use those tools and keep the conditions explicit.
Useful tests include the correct pattern, too few characters, too many characters, a misplaced letter, a misplaced digit and any required case rule.
06 | WHAT IS A CHECK DIGIT?
A check digit is an additional digit calculated from the other digits in a code using an agreed method. It is included so that a later check can detect certain entry or copying errors.
The receiving system checks whether the data digits and the supplied check digit still satisfy that method. If they do not, it rejects the code or flags a possible error.
Check digits are used with identifiers such as product codes and ISBNs. Different systems use different calculations. Use the method supplied in the question rather than assuming one universal rule.
A check digit is not encryption, a secret password or automatic error correction. It adds a consistency test. A failed result does not ordinarily tell the system which digit was intended.
07 | A WORKED METHOD WITH WEIGHTS
We will use an illustrative classroom method, not a claim about the exact algorithm of a particular barcode or ISBN standard.
The payload has four digits. From left to right, multiply them by weights 1, 3, 1 and 3. Add the products. Choose a check digit that makes the combined total a multiple of ten.
A weight is the multiplier assigned to a position. The payload is the original data portion, before the extra check digit is added.
| Payload position | Digit | Weight | Product |
|---|---|---|---|
| 1 | 4 | 1 | 4 |
| 2 | 8 | 3 | 24 |
| 3 | 2 | 1 | 2 |
| 4 | 6 | 3 | 18 |
| Weighted sum | 48 |
For payload 4826, the weighted sum is 48. The next multiple of ten is 50, so the check digit is 2. The complete code is 48262: four payload digits followed by the check digit.
The rightmost 2 is the added check digit. It is not another payload digit to multiply by the next payload weight in this particular method.
08 | VERIFY THE COMPLETE CODE
When code 48262 is entered, separate the four payload digits 4826 from the supplied check digit 2. Recalculate the weighted payload sum, 48, and add the check digit.
The combined total is 50, which is divisible by ten. The code passes this check-digit rule.
MOD produces the remainder after integer division. Fifty MOD ten is zero, so the condition (WeightedSum + CheckDigit) MOD 10 = 0 tests consistency for this method.
If the user instead enters 48362, the payload sum is 4 + 24 + 3 + 18 = 49. Adding the supplied digit 2 gives 51. Its remainder on division by ten is 1, so the code fails.
The system detects an inconsistency. It has not proved whether the error was the third payload digit, the check digit or some other part.
09 | CALCULATE THE DIGIT WITHOUT A SPECIAL CASE
For an integer weighted sum, calculate the check digit as:
CheckDigit ← (10 - (WeightedSum MOD 10)) MOD 10
If WeightedSum is 48, its remainder is 8. Ten minus eight is two, and two MOD ten is two.
If WeightedSum is already 50, its remainder is zero. The final MOD converts ten back to zero, giving check digit 0 rather than an invalid extra digit 10.
For payload 1234, the weighted sum is 1 + 6 + 3 + 12 = 22. The check digit is 8, so 12348 passes because 22 + 8 = 30.
This formula follows our stated method. A different method may use different weights, modulus or rules for its final character.
10 | READ A CHECKING ALGORITHM
The pseudocode below assumes the four payload digits have already been separated into an array, each integer from 0 to 9. CheckDigit is also an integer from 0 to 9. The preparation and digit checks must be performed by the surrounding input routine.
DECLARE Payload : ARRAY[1:4] OF INTEGER
DECLARE CheckDigit : INTEGER
DECLARE WeightedSum : INTEGER
// Assume Payload and CheckDigit are already supplied and checked
// Every element and CheckDigit is an integer from 0 to 9
WeightedSum ← Payload[1] + 3 * Payload[2] + Payload[3] + 3 * Payload[4]
IF (WeightedSum + CheckDigit) MOD 10 = 0
THEN
OUTPUT "Check-digit rule satisfied"
ELSE
OUTPUT "Check-digit rule failed: check the entry"
ENDIFThe algorithm calculates the weighted sum, adds the supplied check digit and tests the remainder. It does not perform a complete format check on arbitrary raw text.
For code 48262, Payload contains 4, 8, 2 and 6, and CheckDigit contains 2. For code 48362, the third payload element is 3 instead.
This separation keeps responsibilities clear: one part checks and interprets the input representation; another performs the agreed arithmetic check.
11 | SOME TRANSPOSITIONS ARE DETECTED, SOME ARE NOT
A transposition error swaps the positions of characters. Weights can make many such changes affect the sum, but they do not guarantee detection of every possible swap.
If 48262 is changed to 84262 by swapping the first two payload digits, the new weighted sum is 8 + 12 + 2 + 18 = 40. Adding the unchanged check digit 2 gives 42, so the code fails.
Now consider payload 0526. Its weighted sum is 0 + 15 + 2 + 18 = 35, giving check digit 5 and complete code 05265.
Swap the first two digits to obtain 50265. The new weighted sum is 5 + 0 + 2 + 18 = 25. Adding the same check digit 5 gives 30, so this different code still passes.
This counterexample is important: the method detects some errors, not all. Do not claim a check digit makes incorrect entry impossible.
12 | A VALID CODE CAN STILL BE THE WRONG CODE
Someone could copy a different complete valid code, including its check digit. It may pass format and arithmetic checks while identifying the wrong item.
Multiple altered digits can also compensate so that the consistency condition remains satisfied. Detection depends on the actual method and the particular changes.
A failed check indicates that the entered digits do not satisfy the rule. The usual response is to reject the entry and request a correction or re-entry, not to guess which digit should change.
Additional measures, such as comparison with a source or checking that the identifier exists in records, address different concerns. A check digit does not replace every other check.
13 | COMBINE CHECKS IN A SENSIBLE ORDER
For our five-digit example code, first require a supplied value. Then check that it contains exactly five digit characters, preserving leading zeros as text.
Only after that preparation should the program separate and interpret the digits for the arithmetic check. Trying to multiply a letter or read a missing character would not be a useful validation response.
| Entered text | Representation | Arithmetic outcome |
|---|---|---|
| 48262 | Five digit characters | Pass: combined sum 50 |
| 48362 | Five digit characters | Fail: combined sum 51 |
| 48A62 | Wrong character pattern | Do not attempt the numeric check |
| 4826 | Wrong length | Do not attempt the five-digit check |
| 50265 | Five digit characters | Pass despite the illustrated transposition |
The exact five-digit numeric pattern checks representation. The weighted calculation checks consistency. A separate record lookup might check whether the identifier is recognised.
Each result has a limited meaning. Together the checks reduce some entry problems, but they still cannot guarantee the user chose the intended valid item.
14 | PRACTISE THE CALCULATION AND EXPLAIN ITS PURPOSE
For payload 2468, the weighted sum is 2 + 12 + 6 + 24 = 44, so check digit 6 gives complete code 24686. For payload 0000, the sum is zero and the check digit is zero.
For a describe question, explain the calculation and later comparison. For an explain question, connect the extra digit to detection: “An entered code is checked using the same agreed rule, so many digit changes produce an inconsistent result and are flagged.”
Compare the two main checks precisely: a format check tests the required character pattern; a check-digit calculation tests an arithmetic relationship between digits.
You should now be able to apply a supplied method, preserve the distinction between payload and check digit, and explain both detection and its limits.