Well, I'm sorry if I got each and every person confused about data
validation, but actually, this question was raised because of my Data
Structures and Algorithms lecturer. She didn't teach us nuts about
validation, and she comes to class saying that we all have to do "data
validation for input".
Consider, for example, an input of a bet amount for a roulette game.
Chips cost $1 each.
If the bet is a valid number (e.g. contains no alphabetic characters),
and the bet is greater than zero and the bet is less than or equal
to the house limit and the bet is an integer and the bet is less
than or equal to the player's bank balance, then the bet amount is
valid.
Reminder: a great way to cheat at poorly-written gambling programs
is to bet a negative amount, using the house advantage against it,
then try to "lose".
Consider, for example, an input which is a telephone number. How
do you validate it?
Hint: isdigit(buffer[0]) returns 0 if the first character in buffer[]
is not a digit, and nonzero if it is.
Try 1: A valid telephone number consists only of digits.
User inputs: 1-800-555-1212
Try 2: A valid telephone number consists only of digits after removing
the characters -, (, ), and blanks.
User inputs: 1-800-555
Try 3: A valid telephone number consists of 10 digits after removing
the characters -, (, ), and blanks.
User inputs: 18005551212
Try 4: A valid telephone number consists of 10 digits after removing
the characters -, (, ), and blanks. If the input is 11 digits and
the first digit is a 1, remove it.
User inputs +44 1277 655112
Try 5: A valid USA telephone number consists of 10 digits after removing
the characters -, (, ), blanks, and the prefix 1 or 011, if present.
We don't care about the rest of the world.
User inputs: 18195551212
Try 6: A valid USA telephone number consists of 10 digits after removing
the characters -, (, ), blanks, and the prefix 1 or 011, if present.
Reject input whose first 3 digits after substitutions is a Canadian
or Mexican area code. Note that this code requires modification when a
new non-USA area code is assigned. We don't care about the rest of the world.
User inputs: 12145551212 Extension 234
<Programmer shoots User>