Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File Input Validation

Status
Not open for further replies.

darkmast2

Technical User
Jul 6, 2001
3
GB
I was wondering if anyone can help me with a little problem i've been having.

I have a perl script which I use to take in a users input from a text box on an html page and then print it into a text file.

I would like to know how to check the users input before sending the data to the text file. I ask this because when they type something in it gets sent to the file okay, but if they don't type something in a blank line gets send to the file. I thought about using an if statement but not sure on how to do it.


Here is my program:

#!/usr/local/bin/perl.exe

#print"HTTP/1.0 200 OK\n";
#print"Content-type: text/html\n\n";

require ("c:/perl/lib/shellwords.pl");

$ASCII_ten = pack("c", 10);
$ASCII_thirteen = pack("c", 13);
$response_page = "
sub Web_Direct {
#Ouput success message.
#
print "Location: $response_page";
}

sub User_Data {
local (%user_data, $user_string, $name_value_pair,
@name_value_pairs, $name, $value);

# If the data was sent via POST, then it is available
# from standard input. Otherwise, the data is in the
# QUERY_STRING environment variable.
#
if ($ENV{'REQUEST_METHOD'} eq "POST") {
read (STDIN, $user_string, $ENV{'CONTENT_LENGTH'});
} else {
$user_string = $ENV{'QUERY_STRING'};
}

# This line changes the + signs to spaces.
#
$user_String =~ s/\+/ /g;

# This line places each name/value pair as a separate
# element in the name_value_pairs array.
#
@name_value_pairs = split(/&/, $user_string);

# This code loops over each element in the name_value_pairs
# array, splits it on the = sign, and places the value
# into the user_data associative array with the name as the
# key.
#
foreach $name_value_pair (@name_value_pairs) {
($name, $value) = split (/=/, $name_value_pair);

# These two lines decode the values from any URL hexadecimal
# encoding. The first section searches for a hexadecimal
# number and the second part converts the hex number to
# decimal and returns the character equivalent.
#
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/ge;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C"),hex($1)/ge;

# If the name/value pair has already been given a value,
# as in the case of multiple items being selected, then
# separate the items with a ":".
#
if (defined($user_data{$name})) {
$user_data{$name} .= ":" . $value;
} else {
$user_data{$name} = $value;
}
}
return %user_data;
};


%data_received = &User_Data;

# Remove any carriage returns or newline characters that
# the user may have entered.
#
foreach $key (keys(%data_received)) {
$data_received{$key} =~s/$ASCII_ten//ge;
$data_received{$key} =~s/$ASCII_thirteen/$space/ge;
}

# Open the text file for appending the information to
# or display error message if unableto open file.
#
open(TEXT, '>>text_file.txt') || die "couldn't open file: $!";

@data_received = %data_received;
foreach $data_received (@data_received)
{
($first, $last) = split('\+', $data_received);
$data_received =~ s/\+/ /gi;
}

# extract the name from the array and print it to the
# text file.
#
print TEXT @data_received[1];


# Print a newline character to the end of the name.
#
print TEXT "\n";

# Close the text file or display an error message
# if the file cannot be closed.
#
close(TEXT) || die "can't close file: $!";

&Web_Direct;
 
You can use a regular expression to check for all whitespace:
Code:
if ( $data_received =~ /^\s*$/ ) {
   # line is all whitespace
}
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top