Solved

perl script won't validate

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl Script to append xml file 32 191
Input from stdin for perl 6 118
Passing shell scripts to run on the SFTP cmd object 12 180
read an xml file in perl 2 14
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

929 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

10 Experts available now in Live!

Get 1:1 Help Now