Solved

perl script won't validate

Posted on 2004-08-26
4
266 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
This video discusses moving either the default database or any database to a new volume.

707 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now