Validation of known and unknown Data Types using Perl
Perl Validation of HTML Form Data – Part 1
Web Development with Perl and MySQL
Foreword: In this part of the series, I explain how to validate known and unknown HTML Form data types.
By: Chrysanthus Date Published: 5 Sep 2016
Introduction
Pre-Knowledge
This series is part of my volume, Web Development with Perl and MySQL. At the bottom of the page you have the series in the volume. From there you should know what you should have studied first before coming here.
The Boolean Datum
In Perl, 1 or a non-empty string that does not have only zero, is equivalent to true. Also, any number except zero is equivalent to true. On the other hand, undef, "", "0" or 0 is equivalent to false. So if you want to test if the variable is equivalent to true or false, the following code will solve the problem:
my $variab = "something";
$variab =~ s/^\s*|\s*$//g; #remove leading and trailing whitespaces
sub istrue
{
if (($_[0] eq undef)||($_[0] eq "")||($_[0] eq "0"))
{
return 0; #for not true
}
else
{
return 1; #for true
}
}
print istrue($variab);
To know if the variable is Boolean (equivalent to 1 or 0), use the following code:
my $variab = "something";
$variab =~ s/^\s*|\s*$//g; #remove leading and trailing whitespaces
sub isBoolean
{
my $bool = 0;
if (($_[0] eq undef)||($_[0] eq "")||($_[0] eq "0"))
{
$bool = 1;
}
if (($_[0] == 1)||($_[0]<0)||($_[0]>0))
{
$bool = 1;
}
if ($bool == 1)
{
return 1;
}
else
{
return 0;
}
}
print isBoolean($variab);
To know whether a variable is a number, you should convert it first to a string and then use regular expression on it. The following code checks whether a variable is a number:
my $variab = "something";
$variab = qq/$variab/; #converting to a string
$variab =~ s/^\s*|\s*$//g; #remove leading and trailing whitespaces
sub isNumber
{
if ($_[0] =~ /\D/)
{
my @arr = $_[0]=~/\D/g;
my $ret = 1;
for (my $i=0; $i<@arr; ++$i)
{
if (!($arr[$i]=~/[\.eE\+-]/))
{
$ret = 0;
return 0;
}
}
return $ret; #for having non digits
}
else
{
return 1;
}
}
print isNumber($variab);
Whole Number
The following code returns 1 if the variable is a whole number and zero if the variable is not.
my $variab = 15;
$variab = qq/$variab/; #converting to a string
$variab =~ s/^\s*|\s*$//g; #remove leading and trailing whitespaces
sub isWholeNumber
{
if ($_[0] =~ /^\d+\z/)
{
return 1
}
else
{
return 0;
}
}
print isWholeNumber($variab);
The following code returns 1 if the variable is an integer and zero if the variable is not:
my $variab = 15;
$variab = qq/$variab/; #converting to a string
$variab =~ s/^\s*|\s*$//g; #remove leading and trailing whitespaces
sub isInteger
{
if ($_[0] =~ /^-?\d+\z/)
{
return 1
}
else
{
return 0;
}
}
print isInteger($variab);
Plus or Minus Integer
The following code returns 1 if the variable is a plus or minus integer and zero if the variable is not:
my $variab = +15;
$variab = qq/$variab/; #converting to a string
$variab =~ s/^\s*|\s*$//g; #remove leading and trailing whitespaces
sub isPlusMinusInteger
{
if ($_[0] =~ /^[+-]?\d+\z/)
{
return 1
}
else
{
return 0;
}
}
print isPlusMinusInteger($variab);
Float Number
The following code returns 1 if the variable is a float number and zero if the variable is not:
my $variab = 17.56;
$variab = qq/$variab/; #converting to a string
$variab =~ s/^\s*|\s*$//g; #remove leading and trailing whitespaces
sub isFloat
{
if ($_[0] =~ /^[+-]?(?=\.?\d)\d*\.?\d*(?:e[+-]?\d+)?\z/i)
{
return 1
}
else
{
return 0;
}
}
print isFloat($variab);
Email Data Type
The following code will return 1 for a valid common email and zero otherwise:
my $variab = "very.common\@example.com";
$variab = qq/$variab/; #converting to a string
$variab =~ s/^\s*|\s*$//g; #remove leading and trailing whitespaces
sub isEmail
{
my $temp = 0;
if ($_[0] =~ /^[0-9a-zA-Z_\.-]{1,64}@[0-9a-zA-Z_-]{1,252}(\.[0-9a-zA-Z_\-]{2,4}){0,2}$/)
{
$temp = 1;
}
if ($temp == 1)
{
if (length($_[0]) <=254)
{
return 1;
}
else
{
return 0;
}
}
else
{
return 0;
}
}
print isEmail($variab);
The following code will return 1 for a valid common URL and zero otherwise:
my $variab = "http://www.broad-network.com/ChrysanthusForcha/Web-Development-Course.htm#PPI";
$variab = qq/$variab/; #converting to a string
$variab =~ s/^\s*|\s*$//g; #remove leading and trailing whitespaces
sub isURL
{
if ($_[0] =~ /^http|https:\/\/([0-9a-zA-Z_\-]{1,64}\.)?[0-9a-zA-Z_\-]{1,64}(\.[0-9a-zA-Z_\-]{2,4}){0,2}(:[0-9]{1,5})?(\/[0-9a-zA-Z_\-]{1,64}){0,64}([0-9a-zA-Z_\-]{1,64}(\.[a-zA-Z]{1,4})?)?(#[0-9a-zA-Z_\-]{1,64})?.*/)
{
return 1;
}
else
{
return 0;
}
}
print isURL($variab);
The IP Data Type
The following code will return 1 for a valid IP address and zero otherwise:
my $variab = "172.16.254.45";
$variab = qq/$variab/; #converting to a string
$variab =~ s/^\s*|\s*$//g; #remove leading and trailing whitespaces
sub isIP
{
if (($_[0] =~ /^[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}\.[\d]{1,3}$/) || ($_[0] =~ /^[\da-fA-F]{1,4}:[\da-fA-F]{1,4}:[\da-fA-F]{1,4}:[\da-fA-F]{1,4}:[\da-fA-F]{1,4}:[\da-fA-F]{1,4}:[\da-fA-F]{1,4}:[\da-fA-F]{1,4}$/))
{
return 1;
}
else
{
return 0;
}
}
print isIP($variab);
You do not really have any data type, like the regex data type. However, if you have to validate any text that does not fall into any of the above category, then use regular expression.
Assume you have the subject,
my $variab = "cork";
and the regex, /c.rk/
then the following code will do the validation:
my $variab = "cork";
$variab = qq/$variab/; #converting to a string
$variab =~ s/^\s*|\s*$//g; #remove leading and trailing whitespaces
sub isRT
{
if ($_[0] =~ /c.rk/)
{
return 1;
}
else
{
return 0;
}
}
print isRT($variab);
That is it for this part of the series. We stop here and continue in the next part.
Chrys
Related Links
Web Development Basics with Perl and MySQLPerl Validation of HTML Form Data
Page Views with Ajax and Perl and MySQL
Web Live Text Chart Application using Perl and MySQL
Search Within a Site using Perl and MySQL
More Related Links
Perl Mailsend
PurePerl MySQL API
Perl Course - Professional and Advanced
Major in Website Design
Web Development Course
Producing a Pure Perl Library
MySQL Course
NEXT