Solved

perl script won't validate

Posted on 2004-08-26
4
271 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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
Perl Scripting from a shell script with if and for 4 88
Using Perl to parse rows 7 107
XPath Syntax help in conjunction with perl XML::LibXML 18 141
Perl string filter 5 88
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…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…

738 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