2. Length validation
Some users will inevitably enter the wrong amount of data, either too long or too short. Length validation checks to see if the value that the user enters is the right length.
Why is length validation important?
"Too short", for example, may include password forms that reject passwords of less than three characters, as it would be too short to be secure. It can include fields where the user enters nothing (length of zero), when the program is expecting at least some data.
"Too long" is often as much a security concern as too short. Some systems, like SQL databases, can be vulnerable to users who insert their own program code into data fields. Implementing a maximum length makes this less likely.
Programs can also make more efficient use of memory if they know the maximum length any piece of data can have.
Example of length validation
The pseudocode below validates the length of an entry
pass ← '' pass_is_valid ← FALSE WHILE pass_is_valid = FALSE OUTPUT('Enter password: must be at least 3 characters long') pass ← USERINPUT() IF LEN(pass)< 3 OUTPUT('Invalid entry. Password must be at least 3 characters long') ELSE pass_is_valid ← TRUE ENDIF ENDWHILE
What the code does:
In the constructs section, we talked about how WHILE loops need a condition to check. So the first two lines are setting the initial conditions. The password variable is empty, and the WHILE loop is told that the blank password is unacceptable.
The loop prompts the user to enter a password, and accepts their input.
It then checks whether the length of the entered password is acceptable.
- If it is shorter than three characters, the loop begins again.
- If the password is an acceptable length, the loop validates the data and allows the program to continue.
Challenge see if you can find out one extra fact on this topic that we haven't already told you
Click on this link: What is length validation?