In today’s lesson, we will learn about Validation, focusing particularly on Length Check, which is one of the most common forms of input validation in programming. Validation simply means checking whether the input entered by the user is acceptable before the program uses it. A length check ensures that the data has the correct number of characters. For example, a PIN must be exactly 4 characters, a username may require at least 6 characters, or a student ID may need to be exactly 8 characters. If the length does not match the required rule, the program should ask the user to enter the value again.
To handle this, we use a WHILE loop, which repeats as long as a condition is TRUE. In validation, the WHILE loop becomes very powerful because it allows the program to keep asking the user for input until it becomes valid. The general idea is:
This ensures that the program does not continue until the user provides correct information.
Let’s look at our first example: validating a 4-digit PIN. The program asks the user to enter a PIN, checks its length, and if it is not equal to 4, the WHILE loop repeats. The pseudocode looks like this:
In this example, the loop repeats as long as the PIN has fewer or more than four characters. The program does not proceed until the user enters a valid four-character PIN.
Now let’s see another example of a length check—this time validating a username that must be at least 6 characters long. If the user enters something shorter, the program will ask again using a WHILE loop. Here is the pseudocode:
DECLARE Username : STRING
OUTPUT “Enter a username (minimum 6 characters):”
INPUT Username
WHILE LENGTH(Username) < 6
OUTPUT “Username too short!”
OUTPUT “Please enter at least 6 characters:”
INPUT Username
ENDWHILE
OUTPUT “Username accepted!”
In this case, the loop keeps repeating while the username is shorter than six characters. The moment the input meets the minimum length requirement, the loop ends and the program continues.
Length validation is extremely important in practical applications. It prevents users from entering incomplete or overly long data, it reduces errors, and it increases security—for example, by enforcing strong passwords or proper ID formats. By using the WHILE loop, we ensure that the program only accepts correct and meaningful data before moving to the next step.
Through these examples, we have learned what a length check is, why it is used, and how a WHILE loop helps us perform validation correctly. These techniques are essential for writing reliable and safe programs, and now you can apply them confidently in your own pseudocode tasks.
