Here is a basic Perl script using CGI to check and see if the required fields are there, and if not, saves an error message into a variable for display back to the user or write to a log file, ect.
code:
#!/usr/local/bin/perl
$error = ""; if ($in{'dd'} eq "") { $error += "<p>Please select from the drop down box.</p>"; } if ($in{'words'} eq "") { $error += "<p>Please include some words in the text box.</p>"; } # ... validating further fields is as easy as continued of the above if statements
if ($error) { print "Content-type: text/html \n\n"; print "<html><head><title>Error</title>"; print "</head><body>"; print "<h2>An Error Has Occurred</h2>"; print $error ; print "Please go back and correct these errors."; print "</body></html>"; } else { # Go on with the CGI ... }
With the Perl CGI above you can validate an HTML form. Note form validation is only what we would consider half of the equation to form imput. Be sure to not only validate but wash or clean the submited imput prior to sending it to your server. Everything must be carefully scanned for mischeivious code and any harmful characters carefully removed. Be sure to see form security, form field washing, form imput cleaning, etc.
075 comments, (1010 reads) All Articles by, GentleGiant
It looks simple, but it works pretty effectively. Can save a lot of time. It is always hard to look for what you do not know. This uncertainty often start to do a lot of irrational behavior and fatal errors. So - very useful. Best regards, TomCtr