Basic Input Validation
Basic Input Validation
Learn how to check user input before processing it, why validation is important, and how basic checks such as required value, type check, range check, length check, and format check help create safer and more reliable programs.
What is Basic Input Validation?
Basic input validation means checking whether the data entered by a user is correct, acceptable, and suitable before the program uses it.
In simple words, input validation is like checking user data at the entrance of a program. If the data is valid, the program continues. If the data is invalid, the program shows an error message and asks the user to enter correct data.
For example, if a program asks for marks, the value should usually be between 0 and 100. If the user enters 150 or -10, the program should reject it because it is invalid.
Easy Real-Life Example
Input Validation as Security Checking
Imagine entering an exam hall. The guard checks whether you have an admit card, whether your name is on the list, and whether you are entering at the correct time.
Similarly, a program checks whether the input data is acceptable before allowing it to be processed.
Why is Input Validation Important?
Input validation is important because users may enter wrong data by mistake, leave fields empty, enter text instead of numbers, or provide values outside the allowed range.
Without validation, a program may crash, calculate wrong results, show confusing output, or behave unexpectedly.
Importance of Input Validation
- It prevents incorrect data from being processed.
- It reduces program errors and crashes.
- It improves user experience by guiding users.
- It helps programs produce accurate results.
- It protects the program from unexpected input.
- It helps maintain data quality.
- It makes programs more reliable and professional.
- It supports safer handling of user-provided data.
Input Validation in the Input → Process → Output Model
Input validation happens after input is received and before processing begins.
| Stage | Meaning | Example |
|---|---|---|
| Input | User enters data. | User enters marks. |
| Validation | Program checks whether data is acceptable. | Marks must be between 0 and 100. |
| Process | Program works with valid data. | Program checks pass or fail. |
| Output | Program displays result. | Program displays result status. |
Simple Validation Flow
INPUT marks
IF marks >= 0 AND marks <= 100 THEN
DISPLAY "Valid marks"
ELSE
DISPLAY "Invalid marks"
END IF
This program checks whether the entered marks are inside the valid range.
Common Types of Basic Input Validation
Basic input validation can be done in different ways depending on the type of data and the program requirement.
| Validation Type | Purpose | Example |
|---|---|---|
| Required Check | Checks that input is not empty. | Name should not be blank. |
| Type Check | Checks that input has the expected data type. | Age should be a number. |
| Range Check | Checks that numeric input is within allowed limits. | Marks should be between 0 and 100. |
| Length Check | Checks the number of characters. | Password should have at least 8 characters. |
| Format Check | Checks whether input follows a specific pattern. | Email should contain a valid format. |
| Allowed Value Check | Checks whether input is one of the accepted options. | Menu choice should be 1, 2, or 3. |
1. Required Check
A required check ensures that the user does not leave an important input empty.
For example, a name field should not be blank in a registration form.
Example
DISPLAY "Enter your name:"
INPUT userName
IF userName IS NOT EMPTY THEN
DISPLAY "Name accepted"
ELSE
DISPLAY "Name cannot be empty"
END IF
This validation prevents empty input from being accepted.
2. Type Check
A type check ensures that the input matches the expected data type.
If the program expects a number, the user should not enter text.
Example
DISPLAY "Enter your age:"
INPUT ageText
IF ageText CAN BE CONVERTED TO INTEGER THEN
SET age = CONVERT ageText TO INTEGER
DISPLAY "Age accepted"
ELSE
DISPLAY "Please enter a valid number"
END IF
This validation checks whether the input can be converted into an integer before using it.
3. Range Check
A range check ensures that a number is within an allowed minimum and maximum value.
Range checks are common for age, marks, quantity, percentage, rating, and price limits.
Example: Marks Validation
DISPLAY "Enter marks:"
INPUT marks
IF marks >= 0 AND marks <= 100 THEN
DISPLAY "Valid marks"
ELSE
DISPLAY "Marks must be between 0 and 100"
END IF
This prevents invalid marks such as -5 or 150.
4. Length Check
A length check ensures that text input is neither too short nor too long.
This is useful for names, usernames, passwords, phone numbers, and ID values.
Example: Password Length
DISPLAY "Enter password:"
INPUT password
IF LENGTH(password) >= 8 THEN
DISPLAY "Password accepted"
ELSE
DISPLAY "Password must be at least 8 characters long"
END IF
This validation checks that the password is not too short.
5. Format Check
A format check ensures that input follows a particular structure or pattern.
Format validation is commonly used for email addresses, phone numbers, dates, postal codes, and identification numbers.
Example: Email Format Concept
DISPLAY "Enter email:"
INPUT email
IF email HAS VALID EMAIL FORMAT THEN
DISPLAY "Email accepted"
ELSE
DISPLAY "Invalid email format"
END IF
This example focuses on the concept. Different programming languages use different techniques for checking formats.
6. Allowed Value Check
An allowed value check ensures that the input is one of the accepted options.
This is useful for menu choices, yes/no answers, status values, and fixed categories.
Example: Menu Choice
DISPLAY "Choose an option:"
DISPLAY "1. Add"
DISPLAY "2. Subtract"
DISPLAY "3. Exit"
INPUT choice
IF choice == 1 OR choice == 2 OR choice == 3 THEN
DISPLAY "Valid choice"
ELSE
DISPLAY "Invalid choice"
END IF
This prevents the program from accepting menu options that do not exist.
Repeating Input Until Valid
In many programs, if the user enters invalid input, the program should ask again instead of stopping immediately.
This is commonly done using a loop.
Example: Keep Asking Until Valid Marks
DISPLAY "Enter marks:"
INPUT marks
WHILE marks < 0 OR marks > 100 DO
DISPLAY "Invalid marks. Please enter marks between 0 and 100:"
INPUT marks
END WHILE
DISPLAY "Marks accepted"
The program continues asking until the user enters valid marks.
Complete Example: Basic Age Validation
/*
This program reads age and validates it.
*/
ENTRY POINT
DECLARE ageText AS TEXT = ""
DECLARE age AS INTEGER = 0
DISPLAY "Enter your age:"
INPUT ageText
IF ageText CAN BE CONVERTED TO INTEGER THEN
SET age = CONVERT ageText TO INTEGER
IF age >= 0 AND age <= 120 THEN
DISPLAY "Valid age"
ELSE
DISPLAY "Age must be between 0 and 120"
END IF
ELSE
DISPLAY "Please enter age as a number"
END IF
END ENTRY POINT
This example uses type validation and range validation together.
Complete Example: Student Marks Validation
/*
This program validates student marks before processing result.
*/
CONSTANT PASS_MARK = 35
ENTRY POINT
DECLARE studentName AS TEXT = ""
DECLARE marksText AS TEXT = ""
DECLARE marks AS INTEGER = 0
DISPLAY "Enter student name:"
INPUT studentName
IF studentName IS EMPTY THEN
DISPLAY "Student name cannot be empty"
ELSE
DISPLAY "Enter marks:"
INPUT marksText
IF marksText CAN BE CONVERTED TO INTEGER THEN
SET marks = CONVERT marksText TO INTEGER
IF marks >= 0 AND marks <= 100 THEN
IF marks >= PASS_MARK THEN
DISPLAY "Result: Pass"
ELSE
DISPLAY "Result: Fail"
END IF
ELSE
DISPLAY "Marks must be between 0 and 100"
END IF
ELSE
DISPLAY "Marks must be a number"
END IF
END IF
END ENTRY POINT
This program validates the name, checks whether marks are numeric, checks whether marks are within range, and then processes the result.
Complete Example: Billing Input Validation
/*
This program validates product price and quantity.
*/
ENTRY POINT
DECLARE productName AS TEXT = ""
DECLARE priceText AS TEXT = ""
DECLARE quantityText AS TEXT = ""
DECLARE productPrice AS DECIMAL = 0.0
DECLARE quantity AS INTEGER = 0
DECLARE totalAmount AS DECIMAL = 0.0
DISPLAY "Enter product name:"
INPUT productName
DISPLAY "Enter product price:"
INPUT priceText
DISPLAY "Enter quantity:"
INPUT quantityText
IF productName IS EMPTY THEN
DISPLAY "Product name cannot be empty"
ELSE IF priceText CANNOT BE CONVERTED TO DECIMAL THEN
DISPLAY "Price must be a valid number"
ELSE IF quantityText CANNOT BE CONVERTED TO INTEGER THEN
DISPLAY "Quantity must be a valid whole number"
ELSE
SET productPrice = CONVERT priceText TO DECIMAL
SET quantity = CONVERT quantityText TO INTEGER
IF productPrice <= 0 THEN
DISPLAY "Price must be greater than 0"
ELSE IF quantity <= 0 THEN
DISPLAY "Quantity must be greater than 0"
ELSE
SET totalAmount = productPrice * quantity
DISPLAY "----- Bill Summary -----"
DISPLAY "Product : " + productName
DISPLAY "Price : " + productPrice
DISPLAY "Quantity : " + quantity
DISPLAY "Total Amount : " + totalAmount
END IF
END IF
END ENTRY POINT
This example shows how validation protects a billing program from invalid product name, price, and quantity values.
Common Beginner Mistakes in Input Validation
Mistakes
- Using input directly without checking it.
- Assuming users will always enter correct data.
- Forgetting to check empty input.
- Forgetting to convert text input into numbers.
- Checking only data type but not range.
- Showing unclear error messages.
- Stopping the program immediately after invalid input.
- Using very complex validation logic too early.
Better Habits
- Validate input before processing.
- Use clear prompts.
- Check required fields.
- Check data type before conversion.
- Check numeric ranges.
- Use meaningful error messages.
- Ask again when input is invalid.
- Keep validation logic simple and readable.
How Input Validation Helps Debugging
Input validation helps debugging because it catches invalid data early before the program uses it in calculations or decisions.
If validation is missing, errors may appear later in the program, making them harder to find.
Debugging Questions
- Did the user enter something?
- Is the input empty?
- Is the input the correct data type?
- Can the input be converted safely?
- Is the value within the allowed range?
- Does the input follow the expected format?
- Is the error message clear?
- Does the program ask again after invalid input?
Best Practices for Basic Input Validation
Good validation should be simple, clear, and close to where input is received.
Recommended Practices
- Validate input before processing it.
- Show clear prompts before reading input.
- Check required fields first.
- Convert input only after checking that conversion is possible.
- Use range checks for numeric values.
- Use length checks for text values.
- Use allowed value checks for menus and fixed options.
- Show helpful error messages.
- Keep asking until valid input is provided when appropriate.
- Keep validation logic readable and easy to test.
Prerequisites Before Learning Basic Input Validation
To understand input validation properly, students should already know a few basic programming concepts.
Basic Prerequisites
- What is input?
- Reading user data.
- Console input and console output.
- Variables and constants.
- Data types.
- Type conversion and type casting.
- Comparison operators.
- Logical operators.
- Conditional statements.
- Loops for repeated input.
Practice Activity: Identify Validation Rules
Read each scenario and write a suitable validation rule.
| Scenario | Validation Rule |
|---|---|
| User enters age. | ________________________ |
| User enters marks. | ________________________ |
| User enters name. | ________________________ |
| User enters menu choice. | ________________________ |
| User enters product price. | ________________________ |
Sample Answers
| Scenario | Validation Rule |
|---|---|
| User enters age. | Age should be a number between 0 and 120. |
| User enters marks. | Marks should be a number between 0 and 100. |
| User enters name. | Name should not be empty. |
| User enters menu choice. | Choice should be one of the available options. |
| User enters product price. | Price should be a positive number. |
Mini Quiz
What is input validation?
Input validation is checking user input before processing it to make sure it is correct and acceptable.
Why is input validation needed?
It is needed to prevent wrong data, program errors, unexpected behavior, and confusing output.
What is a required check?
A required check ensures that important input is not left empty.
What is a range check?
A range check ensures that a numeric value is within allowed minimum and maximum limits.
Give one example of input validation.
Checking that marks are between 0 and 100 is an example of input validation.
Interview Questions on Basic Input Validation
Define basic input validation.
Basic input validation is the process of checking user input for correctness, completeness, type, range, length, or format before processing it.
What is the difference between type check and range check?
Type check verifies whether input is the expected data type, while range check verifies whether a numeric value is within allowed limits.
Why should validation happen before processing?
Validation should happen before processing so that the program does not use invalid or unsafe data.
What is an allowed value check?
An allowed value check ensures that input matches one of the accepted options.
How can invalid input be handled?
Invalid input can be handled by showing a clear error message and asking the user to enter the data again.
Quick Summary
| Concept | Meaning |
|---|---|
| Input Validation | Checking input before processing. |
| Required Check | Ensures input is not empty. |
| Type Check | Ensures input has the expected data type. |
| Range Check | Ensures numeric input is within allowed limits. |
| Length Check | Ensures text has acceptable length. |
| Format Check | Ensures input follows expected pattern. |
| Allowed Value Check | Ensures input is one of the accepted options. |
Final Takeaway
Basic input validation is an essential programming habit. It helps programs accept only correct and meaningful data before processing. In the Programming Mastery Course, students should understand that every user input should be checked using simple rules such as required check, type check, range check, length check, format check, and allowed value check. A reliable program reads input carefully, validates it clearly, processes it safely, and gives helpful output.