CIT 042 Index > 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;
}

Pseudocode

Here is some pseudocode for the assignment

#!/usr/bin/perl

use CGI qw(:standard :debug);
use CGI::Carp qw(fatalsToBrowser);

my $cgi = new CGI();

my $name = $cgi->param("theName");
my $dept = $cgi->param("dept");
my $phone = # similar code
my $color = # similar code
my $number = # similar code

my @error_list = (  );  # no errors in input so far.

#
#	Test all the input for validity
#
if ($name is empty or all whitespace)
{
	push @error_list, "You must enter a name.";
}

if ($dept is not one of BIS, CIT, or MATH)
{
	push @error_list, "$dept is not one of BIS, CIT, or MATH";
}

#
#	Because matching the phone number is so complicated,
#	move the job to a subroutine.
#
if (phone_is_valid($phone) == 0)
{
	push @error_list, "error message about $phone";
}

if ($color contains something other than letters, spaces, and hyphens)
{
	push @error_list, "error message about $color";
}

if ($number is less than 1 or greater than 100)
{
	push @error_list, "error message about $number";
}

#
#	When we get here, if everything is OK,
#	@error_list will still be empty.  If we had
#	any errors, they are all in @error_list

print "Content-type: text/plain\n\n";

if (@error_list == 0)
{
	#	Everything's good.
	#	echo back all the data as in the example in the assignment.
	#
}
else
{
	# There are errors.
	print "You have input errors.\n";
	
	foreach my $item (@error_list)
	{
		print "* $item\n";
	} 
}


#
#	This subroutine returns 0 if there is no
#	match for the phone number, 1 if the phone
#	number is valid
#
sub phone_is_valid
{
	my $phone = shift;
	my $result = 0;		# presume that it's wrong
	
	if ($phone matches first pattern)
	{
		$result = 1;
	}
	elsif ($phone matches second pattern)
	{
		$result = 1;
	}
	elsif ($phone  matches third pattern)
	{
		$result = 1;
	}
	return $result;
}