1. Computing & Technology

Discuss in my forum

Perl CGI Form Validation

Use a Perl CGI Script to Validate an HTML Form

By , About.com Guide

CGI for Validating Forms

This 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 += "<p>Please select from the drop down box.</p>";
}
if ($in{'words'} eq "")
{
  $error += "<p>Please include some words in the text box.</p>";
}
# ... continue validating all fields

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 paragraph tag 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.

This article is part of the HTML Forms Tutorial

©2012 About.com. All rights reserved.

A part of The New York Times Company.