Send Close Add comments: (status displays here)
Got it!  This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
Data validation


1. Data validation

2. Bad data
Once bad data gets into a system, such as a database, it is very difficult to find and remove it. When copies of that bad data are made, it gets even harder.

It is a good idea to keep bad data out of a system.

Letting users enter any data they want into a system can cause problems.

3. Program and users
Many of our programs have assumed that another program is providing the data and that that program kept bad data out of this program.

At the user interface, bad data needs to be kept out.

4. Web systems
In many web systems, users are not allowed to proceed until the data meets certain requirements.

5. Validation
In programming, the term validation refers to checking that what the user typed was expected. If not, the user is asked to provide additional input.

The related term verification refers to ensuring that the program is correct according to specifications.

6. Validation and verification
In validation, the data needs to meet a certain format.

It is not, in general, possible to actually check that email addresses, phone numbers, etc., are, in reality, the correct ones when entered.

For example, one way to verify an email address is to send a message with a code in a link to the email address. The user then needs to click on the link to verify that the email account actually exists. (But, the user may be a machine/bot and not human - another issue).

7. Pseudo-code
Here is the pseudo-code that uses a loop for getting data and validating it.
set valid is false while not valid do    ask for data d1    get data d1    if data is valid then       set valid to true    else       tell user the input was not valid       end if    end while

Note: C does not directly support true and false so 1 is usually used for true and 0 is used for false.

8. C program showing validation
Here is a C program that shows validation.

Here is the C code.


9. Examples of input and output
Here are some examples of input and output for the above program code.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.


10. Actual programs
Note that the example above uses a loop.

In actual programs, the above would never be written.

Instead, the program would validate the data and, if there is a problem, raise an error or just halt the program with an appropriate error message.

On a web page, for example, an error message would be displayed and no action would take place until the input data is validated.

11. End of page

12. Multiple choice questions for this page