Server-Side Validation Script

#!/usr/local/bin/perl -w

# load CGI module
use CGI;

# create a CGI object
$q = new CGI;


# start creating output
# first, the HTTP header
print $q->header();

# extract form data;
# the function parameter is the name 
# of the form field
$field = $q->param('text_sent');

# create HTML page
print $q->start_html(-title=>"First Server-Side Validation",
                        -bgcolor=>"ivory");

$type = "";

# first condition: test string contains if any 
# non-digit characters or is empty
if ($field =~ m/\D/ || $field eq "")
{
  # check if string is empty or doesn't contain anything
  # but white space characters (space, tab, etc.)
    if ($field eq "" || $field !~ m/\S/)
    {
      $type = "an empty string";
    }
    else
    {
      $type = "a string";
    }
}
# string is not empty and contains only
# digits ==> must be an integer
else
{
  # length() is a string function that returns
  # the number of characters
    $type =  "an integer of " . length($field) . " digits";
}
    print $q->h3("Received $type");

    if ($type ne "an empty string") 
    {
      # turns string into upper case;
      # has no effect on numbers
      print uc($field) . "\n";
    }

# close HTML page
print $q->end_html();