1. Home
  2. Computing & Technology
  3. Web Design / HTML

Perl CGI Form Validation
Use a Perl CGI Script to Validate an HTML Form

By Jennifer Kyrnin, About.com Guide

CGI

The Perl script CGI snippet does the same thing as the JavaScript. It checks to see if the required fields are there, and if not, saves an error message into a variable for display:

#!/usr/local/bin/perl

$error = "";
if ($in{'dd'} eq "")
{
  $error += "Please select from the drop down box.<p>";
}
if ($in{'words'} eq "")
{
  $error += "Please include some words in the text box.<p>";
}
...

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 ...
}

The difference with how the CGI writes the error message is that instead of a "\n", it uses the HTML code <p> to put a new line in between each error.

And Now You've Validated Your Form

With the two methods, CGI and JavaScript, you've validated an HTML form so that more of the parts that are sent to you are accurate.

Explore Web Design / HTML
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Web Design / HTML
  4. HTML and XHTML
  5. XHTML
  6. Forms
  7. HTML Form Validation - Validating Forms with JavaScript or CGI - Using a Perl CGI Script to Validate a Form>

©2009 About.com, a part of The New York Times Company.

All rights reserved.