6.4.3 – Validate input
Choose checks that match an input rule, put them in a safe order and ask again when data is unsuitable. You will use presence, length, range and pattern checks, and distinguish valid data from a true fact.
What validation establishes
Validation checks whether data satisfies the rules a program requires before it accepts the data. A booking program might allow a party size from 1 to 6. Rejecting 0 prevents a booking with no people; rejecting 9 prevents a booking too large for the table.
Passing the check means valid according to those rules. It does not prove that the data is true: someone can enter 4 when their party actually contains 3 people. Validation also does not establish a user's identity; checking a supplied ID and password against stored details is authentication.
| Check | What it establishes | Example requirement |
|---|---|---|
| Presence | Required input is not missing | A name is not empty or spaces only |
| Length | There are an allowed number of characters | A label has 3 to 12 characters |
| Range | A number lies between specified limits | A party size is 1 to 6 inclusive |
| Pattern | Characters follow the required arrangement | Two uppercase letters, then three digits |
Start with the requirement, then choose the check. Not every field needs all four. A three-digit suffix in an identifier is text: 007 may be permitted and must keep its leading zeros. Do not invent a numeric range for it. If a task guarantees valid whole-number input and says no validation is required, use that assumption rather than adding unrelated rejection rules.
Presence, length and fresh input
Keyboard input is a string. An empty submission is "". If spaces-only input must also be rejected, remove spaces from both ends before checking for emptiness. This does not change the original string unless the result is assigned back to it.
name = input("Name: ")
missing = name.strip(" ") == ""
A length check uses the number of characters returned by len(). Spaces count. For example, "A B" has three characters. The rule must specify whether it measures the entered text or a cleaned version.
A display name must contain 3 to 12 entered characters and at least one non-space character:
name = input("Display name: ")
while name.strip(" ") == "" or len(name) < 3 or len(name) > 12:
print("Enter 3 to 12 characters, not spaces only.")
name = input("Display name: ")
print("Accepted: " + name)
The condition lists ways the input can be invalid. Join these failures with or: any one is enough to reject it. The loop reads and stores fresh input each time. Without that assignment, an invalid name would never change and the loop could not finish.
A short name followed by a valid name
Example runDisplay name: AI
Enter 3 to 12 characters, not spaces only.
Display name: Nova7
Accepted: Nova7
Range checks and boundaries
An inclusive range includes both endpoints. A rating from 1 to 5 is valid at 1 and at 5. Validity needs both limits to hold; invalidity needs only one limit to fail.
valid = rating >= 1 and rating <= 5
invalid = rating < 1 or rating > 5
A value cannot be below 1 and above 5 simultaneously. Joining the two invalid comparisons with and would reject nothing.
In this focused example the user is guaranteed to enter text that converts to an integer. It checks the numeric range only. Do not use this version when letters or empty input are possible.
Explore inclusive boundaries
{"exercise_id":"cs-c11-range","task_version":"1","code":"rating = int(input("Rating (1 to 5): "))\nwhile rating < 1 or rating > 5:\n print("Out of range")\n rating = int(input("Rating (1 to 5): "))\nprint("Accepted")","instructions":"Run with 0, then 1. Run again with 6, then 5. Explain why the two boundary values finish the loop. For this exercise enter integers only."}
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account