大象传媒

Simple date validation

A programmer should consider that any inputs a user makes may be incorrect and should plan for such unexpected actions. Using helps a programmer to ensure that any input is possible and sensible.

Validation applies rules to inputted data. If the data does not follow the rules, it is rejected, reducing the risk that incorrectly input data may crash a program.

A programmer can build various types of validation into a program:

  • Range check - the input must fall within a specified range. This is usually applied to numbers and dates, but can apply to characters too. For example, when a payment needs to be made to someone, the amount to be entered must be set to be greater than zero and not greater than the available funds.
  • Length check - the input must not be too long or too short. For example, a surname will require at least one letter, but is unlikely to require more than 40.
  • Presence check - a data value must be entered. For example, a quantity is required when placing an order.
  • Format check - the data must be in the correct format. For example, a date must be in the format DD/MM/YYYY.
  • Type check - the data must be of a specified data type, such as an when a quantity is to be specified.

Validation does not ensure that the data entered is correct, just that it is possible and sensible. A user may accidentally enter a date of birth that is possible and sensible, but incorrect. The program has no way of knowing that the date has been entered incorrectly.

To get around this, many programs include verification checks - they repeat the entered data to the user and ask them to confirm if this data is correct. If the user confirms the data, the program then assumes it to be correct. This is an example of how planning the design of a program can help reduce errors.

This simple validation program will iterate until the user enters a correct response:

response 鈫 ""
WHILE response <> "y" AND response <> 鈥渘鈥
     OUTPUT 鈥淒o you want to proceed? y/n鈥
     response 鈫 USERINPUT
     response 鈫 TOLOWER(response)
ENDWHILE
OUTPUT "Okay, let's continue..."