CGI Form validation help required

They have: 1 posts

Joined: Mar 2006

Hi Guys,

I have one html form and users will fill this form once users finish and click send we have a cgi script to take all the information to send an email.Now i want to validate this form field validation like name box only allow charates,for email check for dot and @ symbol etc.. for all fields but i don't know how to do this .I am giving my cgi code can any one tell me how to validate all the fields available in form

#!/usr/bin/perl
#
# mailto CGI

#=============================================
$mailto = "test\@domain.net";
$subject = "inquiry";
#=============================================

use CGI;
CGI::ReadParse(*data);

### path to sendmail ###
$sendmail = "/usr/local/bin/nkf -j | /usr/sbin/sendmail";

$delivery = $data{'delivery'};
$dial = $data{'dial1'};
$isdn = $data{'is1'};
$direct = $data{'dir1'};
$serve = $data{'serve'};
$storage = $data{'st1'};
$name = $data{'name'};
$address = $data{'address'};
$phone = $data{'phone'};
$email = $data{'email'};
$fax = $data{'fax'};
$message = $data{'message'};
$occupation = $data{'occupation'};
$title = $data{'title'};
$postcode = $data{'postcode'};
$country = $data{'country'};
$provider = $data{'provider'};
$status = $data{'status'};
$media = $data{'media'};
$other = $data{'other'};

$address =~ s/^\n$//;
$message =~ s/^\n$//;

if ($delivery eq "fax") {
if (
$delivery &&
($dial1 || $is1 || $dir1 || $st1 || $se1) &&
$title && $name && $phone && $fax
) {
&sendmail();
&delivery_message();
} else {
&print_error();
}
}
elsif ($delivery eq "—X‘—") {
if (
$delivery &&
($dial1 || $is1 || $dir1 || $st1 || $se1) &&
$title && $name && $phone && $address && $country
) {
&sendmail();
&delivery_message();
} else {
&print_error();
}
}
else {
&print_error();
}

thanks for your help

timjpriebe's picture

He has: 2,667 posts

Joined: Dec 2004

Here's some sample code that may be the type of thing you're looking for. Also, there's a decent guide to Perl's regular expressions here:

http://www.troubleshooters.com/codecorn/littperl/perlreg.htm

# Create blank error message so we can check at the end if there were any errors.
my $errormessage = '';


# Check the address to simply see if there's anything there
if ($address eq '')
{
# Failed the check, so see if we need to add a comma to the error message
if ($errormessage ne '')
{
$errormessage .= ", ";
}
$errormessage = "Address";
}

# Check the name to make sure it only uses alphabetical characters and spaces, and
# that there's at least one character (ie it's not blank)
if (!($name =~ /^[A-z\s]+$/))
{
# Failed the check, so see if we need to add a comma to the error message
if ($errormessage ne '')
{
$errormessage .= ", ";
}
$errormessage = "Name (blank or invalid character)";
}

# Check the phone number to make sure there's at least 7 characters, and they are
# either numbers, dashes, open parenthesis or close parenthesis
if (!($phone =~ /^[0-9\-()]{7,}$/))
{
# Failed the check, so see if we need to add a comma to the error message
if ($errormessage ne '')
{
$errormessage .= ", ";
}
$errormessage = "Phone (blank or invalid format)";
}

# Check the email to make sure the beginning has valid characters and that there's at
# least one character. After that, there should be an @, then more valid characters
# (one or more), then a period, then 2-4 alphabetical characters
if (!($email =~ /^[A-z0-9_\-]+\@(A-z0-9_-]+\.)+[A-z]{2,4}$/))
{
# Failed the check, so see if we need to add a comma to the error message
if ($errormessage ne '')
{
$errormessage .= ", ";
}
$errormessage = "Email (blank or invalid format)";
}

if ($errormessage ne '')
{
print 'The following fields had errors: ' . $errormessage;
exit;
}
'

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.