Input Validation
Input Validation prevents a user for entering incorrect or invalid data into a program. It does this by checking if the data is correct. If the data is incorrect, an error is displayed and the user is asked to enter the data again. This repeats over and over until the data is correct or valid.
Key Points
Input Validation prevents a user from entering incorrect data.
It uses the same four lines of code each time.
The code will not progress until a valid input has been entered.
What does Input Validation look like?
Input Validation is almost always four lines of codes and consists of the following.
- Ask the user for an input
- Using a conditional loop, check if the input is valid
- While the input is invalid, display an error message
- While the input is invalid, ask the user for another input
After the user is asked for another input, the code returns back to step 2 to check if the input is valid and the process repeats.
Below is an example of how this code looks in Python.
option = str(input("Enter yes to continue... "))
while option != "yes":
print("ERROR: You must type yes to continue!")
option = str(input("Enter yes to continue... "))