# Data Validation Checks (Range, Length, Type, Presence)

- **Track:** Cambridge IGCSE™ Computer Science (0478 / 0984) (CAMBRIDGE IGCSE 0478)
- **Module:** Section 7: Algorithm Design & Problem-Solving
- **Estimated Duration:** 15 minutes
- **Access:** Open Theory (All-Access for Interactive IDE &amp; Autotests)

---

## Theory & Conceptual Background

**Validation** is an automatic computer check performed by software to ensure that data entered into a system is **sensible**, **reasonable**, and **conforms to predefined rules** before it is processed or committed to a database.

---

## 1. The Critical Distinction

> [!NOTE]
> **Exam Golden Rule**:
> Validation **DOES NOT** check if data is factually correct.
> It only checks whether the data is acceptable according to the rules of the system.
> For example: If a student types their name as "Dvaid" instead of "David", a validation check will accept it because "Dvaid" satisfies presence, length, and character-type checks!

---

## 2. The Six Standard Validation Checks

  
| Validation Check | Definition / Rule Tested | Concrete Example |
| --- | --- | --- |
| **Range Check** | Verifies that a numerical or date value falls between an upper and lower boundary. | Month must satisfy $1 \le \text{Month} \le 12$. Exam score $0 \le \text{Mark} \le 100$. |
| **Length Check** | Verifies that a string contains either an exact number of characters or falls within minimum/maximum limits. | Password must be $\ge 8$ characters. PIN text must contain exactly 4 characters; a separate format check can require digits. |
| **Type Check** | Verifies that the value has the expected data type. | Age must be an integer. A STRING type alone does not restrict a surname to letters; a separate format rule is needed. |
| **Presence Check** | Verifies that a mandatory data field has not been left blank or empty. | National Insurance number or Email address field cannot be empty. |
| **Format Check** | Verifies that text conforms to a specific pattern or character mask. | Postal code matching `LLN NLL` (e.g. `CB2 1TN`). Date matching `DD/MM/YYYY`. |
| **Check Digit** | A calculated value appended to the end of a numerical code to detect transmission, transcription, or transposition errors. | Final digit of an ISBN-13 book code or EAN-13 barcode recalculated via modulo arithmetic. |

---

## 3. Implementing Validation in Cambridge Pseudocode

Input validation loops ensure incorrect entries are rejected immediately and re-prompted:

```pseudocode
DECLARE StudentAge : INTEGER

REPEAT
    OUTPUT "Enter student age (11 to 18): "
    INPUT StudentAge
    IF (StudentAge < 11) OR (StudentAge > 18) THEN
        OUTPUT "Invalid age! Must be between 11 and 18."
    ENDIF
UNTIL (StudentAge >= 11) AND (StudentAge <= 18)

OUTPUT "Valid age recorded: ", StudentAge
```

Notice that the `REPEAT ... UNTIL` loop checks the **valid** condition at the bottom to exit.

---

## Interactive Exercises & Autotest Specifications

### Exercise 1: Exam-Style Check: Match Validation Checks to Data Input Scenarios (Matching terms, 25 XP)

**Instructions:**
Match each validation check to the scenario where it provides the primary defense.


**Starter Code:**
```text
{"prompt":"Match each validation check with its application scenario:","left":[{"id":"chk_range","text":"Range check"},{"id":"chk_length","text":"Length check"},{"id":"chk_presence","text":"Presence check"},{"id":"chk_format","text":"Format check"},{"id":"chk_check_digit","text":"Check digit"}],"right":[{"id":"scen_range","text":"Ensuring an exam score falls between 0 and 100"},{"id":"scen_length","text":"Ensuring a telephone-number text field contains exactly 10 characters"},{"id":"scen_presence","text":"Ensuring an email address field is not left blank on registration"},{"id":"scen_format","text":"Ensuring a date of birth matches the pattern DD/MM/YYYY"},{"id":"scen_check_digit","text":"Recalculating a final barcode digit to detect misread numbers"}]}
```

**Autotest Expectations:**
- **Validation checks matched to input scenarios**: `match_pairs` -> Expected: `{&quot;chk_range&quot;:&quot;scen_range&quot;,&quot;chk_length&quot;:&quot;scen_length&quot;,&quot;chk_presence&quot;:&quot;scen_presence&quot;,&quot;chk_format&quot;:&quot;scen_format&quot;,&quot;chk_check_digit&quot;:&quot;scen_check_digit&quot;}`

### Exercise 2: Exam-Style Question: Classify Data Validation Checks (Fill in blank, 35 XP)

**Instructions:**
Identify the most appropriate validation check (Range check, Length check, Type check, or Presence check) for each requirement:

1. Ensuring an examination percentage mark is between 0 and 100: [blank_1]
2. Ensuring a postal code contains between 6 and 8 characters: [blank_2]
3. Ensuring an age field contains numeric digits only and no alphabetic characters: [blank_3]
4. Ensuring a mandatory email address input box is not left blank: [blank_4]


**Starter Code:**
```text
{"prompt":"Identify the validation check:","template":"1. Mark between 0 and 100: [blank_1]\n2. Postal code between 6 and 8 characters: [blank_2]\n3. Age must be an INTEGER: [blank_3]\n4. Field not left empty: [blank_4]","word_bank":["Range check","Length check","Type check","Presence check","Format check","Check digit","Lookup check"],"blanks":[{"id":"blank_1"},{"id":"blank_2"},{"id":"blank_3"},{"id":"blank_4"}]}
```

**Autotest Expectations:**
- **Mark check is Range check**: `text_exact` -> Expected: `Range check`
- **Postal code check is Length check**: `text_exact` -> Expected: `Length check`
- **INTEGER data type is Type check**: `text_exact` -> Expected: `Type check`
- **Non-blank is Presence check**: `text_exact` -> Expected: `Presence check`

### Exercise 3: Independent Practice: Accept a valid mark (Pseudocode, 30 XP)

**Instructions:**
Read integer marks until one lies in the inclusive range 0 to 100. Output the first valid mark.

Write a complete program. Read inputs in the stated order. Output only the requested results, one per line, without input prompts. Test with your own values before submitting.


**Starter Code:**
```text
// Plan your solution, then write your program.

```

**Autotest Expectations:**
- **Both boundaries invalid first**: `test_case` -> Expected: `{&quot;inputs&quot;:[-1,101,0],&quot;output&quot;:&quot;0&quot;}`
- **Upper boundary**: `test_case` -> Expected: `{&quot;inputs&quot;:[100],&quot;output&quot;:&quot;100&quot;}`
- **Ordinary value**: `test_case` -> Expected: `{&quot;inputs&quot;:[-4,56],&quot;output&quot;:&quot;56&quot;}`

