CIT042 Index > Assignment - CGI

Assignment - CGI

Read everything before doing anything.

Write a CGI script that validates the input in the form which you can see (and try out) at this linked page. You must use this file as your base HTML file.

Here are the requirements for your validation.

When you click the “Send Data” button, your script will validate the form data. If there are any errors, they must all appear in the resultant output. area. If there are no errors, you will echo back the data in any form that you feel appropriate.

In your error output, you must distinguish between:

You must use CGI; to extract the information from the form.

You must use regular expressions in your validation code, though you don’t need to use it for every single conditon.

You may see what the finished program should do. (Try entering bad data to see how it responds.)

Notes & Hints

Generating the error messages

  1. Create an array named @error_list
  2. Every time you get an error in validation,
    push @error_list, "your error message"

When you finish doing all your tests of all the input fields, look at the size of @error_list. If there are no items in the array, you have no errors, so you give the feedback of all the information. If there are any items in the array, do code like this:

print "<ul>\n"; # start a bulleted list
foreach $item (@error_list)
{
	print "<li>$item</li>\n";
}
print "</ul>\n"; # end bulleted list

Trimming Blanks

Use the following code to remove leading and trailing blanks from a string:

sub trim
{
	my $str = shift @_;	# do you know why this works?!

	#	trim leading blanks by replacing one or more
	#	blanks at the start of the string with nothing.
	$str =~ s/^\s+//;

	#	trim trailing blanks by replacing one or more
	#	blanks at the end of the string with nothing.
	$str =~ s/\s+$//;

	return $str;
}