Solved

perl script won't validate

Posted on 2004-08-26
4
269 Views
Last Modified: 2012-06-27
I am trying to get my form to validate. The script should test the input and if there is an error display the @errors, but if the input is correct display a page that shows the results. Everything works except the validation part. When I test it, I don't get any syntax errors. Is there aproblem with my If statement?

The webpage: http://crux.baker.edu/tscott04/public_html/chap06/c06case1.html

The Script:

#assign values to variables
$name = param('Name');
$proj1 = param('Proj1');
$proj2 = param('Proj2');
$midterm = param('Midterm');
$final = param('Final');
$total_score = ($proj1 + $proj2 + $midterm + $final);

#validate input data
if ($proj1 > 50 and $proj1 == "") {
      push(@errors, "Please enter a score between 0-50 for Project 1.");
}
 
if ($proj2 > 50 and $proj2 == "") {
      push(@errors, "Please enter a score between 0-50 for Project 2.");
}


if ($midterm > 100 and $midterm == "") {
      push(@errors, "Please enter a score between 0-100 for Midterm.");
}


if ($final > 100 and $final == "") {
      push(@errors, "Please enter a score between 0-100 for Final.");
}

#determine size of @errors array
$size = @errors;

#process input data or display error page
if ($size == 0) {
      #process input data
#print name and final score to file
open(OUTFILE, ">> c06case1.txt")
      or die "Error opening c06case1.txt. $!, stopped";
print OUTFILE "$name, $total_score\n";
close(OUTFILE);

#generate HTML acknowledgement
print "<HTML>\n";
print "<HEAD><TITLE>Mountain Community College</TITLE></HEAD>\n";
print "<BODY>\n";
print "<H3>A record for $name with a total of $total_score has been saved.</H3>\n";
print "</BODY>\n";
print "</HTML>\n";

}
else {
#display error page
      print "<HTML><HEAD><TITLE>Mountain Community College</TITLE></HEAD>\n";
      print "<BODY>\n";
      print "<H3>Please use your browser's back button to \n";
      print "return to the form to make the following corrections: </H3><BR>\n";
      for(my $x=0; $x<$size; $x+1) {
            print "$errors[$x]<BR>\n";
      }
      print "</BODY></HTML>\n";      


}

0
Comment
Question by:reesecup999
  • 3
4 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
ID: 11908840
$proj1 == ""
is a strange way to write
$proj1 == 0

$proj1 > 50 and $proj1 == ""
will never be true
0
 
LVL 84

Expert Comment

by:ozo
ID: 11908901
Perhaps you meant "or" instead of "and"
0
 
LVL 84

Expert Comment

by:ozo
ID: 11908943
Perhaps you also meant "eq" instead of "=="
0
 
LVL 48

Expert Comment

by:Tintin
ID: 11909329
Given that it's an assignment, we can't feed you too much (ozo has already given you plenty).

If you convert the if logic into English you get:

If proj1 is greater than 50 and is blank then set an error message.

Unless you live in a very Zen world, I'm sure you can see the flaw in the logic.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
binary to char / hexadecimal 5 120
Vb script to unzip a files and rename the files 12 113
Ned Perl Snippet to Read Files in Directory 5 74
Perl script to delete older files 6 88
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question