Programming and validation

As we know input must be validated otherwise our programs may exhibit logic or runtime errors. Validation in programming is exclusivly handled through boolean logic. This can be done in IF statements or in the condition of a while loop. Let us consider a length check. This is where we do not allow input which has too many letters.

if LENGTH(input) >= 10 THEN PRINT "The input has too many letters"

We can do a range check using a IF statements as well-

if a<10 OR a>40 THEN PRINT "The input must be between 10 and 40."

We can continually ask for input until it is correct by the follwing code -

REPEAT
    a = INPUT "please eneter a number between 10 and 40"
UNTIL a=> 10 AND a<=40

The loop will repeat until we have valid data.

Data type checks can also be done but it is more difficult. If you assume that input is always a string we can convert the string into another data type (say a integer). If there is a error in the conversion process then the data type check will fail and a new input can be requested. This can be done by parsing the input or waiting for a run time error and handling the exception. You are not required to write code for data type checks or check digits!