Ekanvitha9 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,I have crated a registration form in HTML and calling the arrugemts in Perl script.I'm running this code in IIS server.This is my HTML code

<!DOCTYPE html> <html> <H3>Registration Form </H3> <body> <form name="myform" METHOD="POST" ACTION="app.pl" > <p> <label for="employee name">Employee Name</label> <input type="text" id="employee name" name="employee n +ame"/><br> </p> <p> <label for="password">Password </label> <input type="password" id="password" name="password" r +equired/> </p> <p> <label for="dob">DOB </label> <input type="date" id="dob" name="dob" required /> </p> <p> <label for="Gender">Gender</label> <input type="radio" id="gender" name="gender" value="m +ale" required /> Male <input type="radio" id="gender" name="gender" value="f +emale" required/> Female <input type="radio" id="gender" name="gender" value="o +ther" required/> Other </p> <p> <label for="phone number">Phone number</label> <input type="tel" id="Phonenumber" name="Phonenumber" +required/> </p> <p> <label for="emailid">EmailId</label> <input type="email" id="emailid" name="emailid" /> </p> </form> </body> </html>

This is the perl script

#!usr/bin/perl use strict; use warnings; use CGI qw(:standard); print "content-type:text/plain\n\n"; my $query = CGI->new(); my $employee=$query->param("employee name"); print("$employee\n"); my $password = $query->param("password"); print ("$password\n"); if ($password != /^[\w\d@#\$%`~!^&*()_\-+={}[\]|\\'";:\/?.><,]{8}$/ ) { print("please enter correct password\n"); } my $Phonenumber = $query->param(Phonenumber); my@ph=split('',$Phonenumber); my $size=@ph; if($size==11 ){ if($Phonenumber=!/^[6-9]+\d/) { print("Please enter valid phone number"); } } my $emailid = $query->param("emailid"); print ("$emailid");

While entering the password and submitting,I'm unable to validate.Please tell me where I'm doing wrong.

Replies are listed 'Best First'.
Re: validations in perl
by Corion (Patriarch) on Jul 16, 2019 at 15:15 UTC

    Is the CGI part necessary to check the password validation?

    Please change your code for debugging and remove all the CGI parts. This allows you to easily see the warnings that Perl throws. These warnings will point you o this line:

    if ($password != /^[\w\d@#\$%`~!^&*()_\-+={}[\]|\\'";:\/?.><,]{8}$/ )

    The binding operator for regular expression matches is ~, not =. So you most likely want !~ there, instead of !=.

      I have changed != to !~, but no change.The user given password,phone number arguments are calling in perl script for validation.So CGI is necessary in my view.

        I don't think that the CGI parts are necessary for finding out why your code does not work.

        You don't show us how your code fails.

        Please show us code that runs for given values of $employee and $password and fails to do what you think it should do. Please also explain in written English what you expect the program to do, and also how it fails to do what you want.</c>

Re: validations in perl
by choroba (Cardinal) on Jul 16, 2019 at 15:13 UTC
    != is the "not numerically equal" operator. You probably meant !~, the negation of the binding operator.

    Morevoer, =! is an assignment followed by a negation. It seems you wanted !~ again.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: validations in perl
by davido (Cardinal) on Jul 17, 2019 at 14:53 UTC

    There is a tendency when people develop CGI scripts to toss all the business logic (the model), all the support logic (such as validation), all the rendering code (the printing, the HTML generation, the view) into the same ball of mud with the CGI handling (params, dispatch handling, the application controller). The result is a hard to test mess. This code is going down that path, and you're experiencing the pain it can cause.

    Break your validation out into a separate testable module. Separate out your rendering. Divorce the business logic from the CGI handlers. You'll find that by doing so you'll be able to leverage the good parts of Perl, like its rich set of testing tools. When you write tests against your validation code, you'll better understand why the validation is failing to do what you want.

    Have a look at the book Intermediate Perl for a good discussion on modules and testing.


    Dave

Re: validations in perl
by Don Coyote (Hermit) on Jul 17, 2019 at 14:03 UTC

    Hello Ekanvitha9 and welcome to the Monastery

    The x modifier can help make matches readable in code. With this modifier any whitespace is not compiled into the pattern, unless escaped using the \s class.

    #!/usr/bin/perl -T; use strict; use warnings; my $password = 'a!1"a$234]5%}&<AcdEf^&B*#$")'; $password =~ /\A # start of string ( # start capture [ # start character class A-Z a-z _ # alpha letters (\w) 0-9 # numbers (\d) > & { } < \$ [ # specials \] ! " % ) ^ * # ] # end character class {28} # match exactly 28 characters ) # end capture \z # end of string /x; # match flags $password = $1 or die send_log('password_reset'); print "$password\n"; sub send_log{ my $log_type = shift; $log_type =~ s/\A([A-Za-z][A-Za-z0-9_]{7,20})\z/$1/ or die; if($log_type eq 'password_reset'){ print "The first part of a new password has been sent to\n", "your inbox. Call facilities on the internal line\n", "to recieve the next part. You will also recieve\n", "instructions for locating the pigeon wearing the\n", "last part.\n"; # exit( release_pigeon( ++$pc ) ); } exit; }

    Hope this helps. To go back and see your earlier posts, with lots of good advice about this kind of thing, just click on the write ups number on your home node.


      With this modifier any whitespace is not compiled into the pattern, unless escaped using the \s class.

      Nitpick: "A single /x tells the regular expression parser to ignore most whitespace that is neither backslashed nor within a bracketed character class." - from perlre (emphasis mine).

Re: validations in perl
by karlgoethebier (Abbot) on Jul 18, 2019 at 15:20 UTC
Re: validations in perl
by Don Coyote (Hermit) on Jul 30, 2019 at 08:25 UTC

    CGI : FETCHING-ENVIRONMENT-VARIABLES.

    For future reference, there is a documented issue regarding IIS server and some CGI methods.

    The issue relates to methods starting with path_ that fetch environment variables.

    Basically do not use them in CGI scripts on IIS server. As they do not work as expected.

Re: validations in perl
by Anonymous Monk on Jul 18, 2019 at 01:57 UTC
    > Please tell me where I'm doing wrong.

    1. Consider using Mojolicious instead of CGI.pm

    2. This is weird:

    use CGI qw(:standard); print "content-type:text/plain\n\n"; my $query = CGI->new();
    Do this instead;
    use CGI qw(:standard); my $query = CGI->new(); print $query->header('text/plain');
    3. Should the phone number only start with digits 6, 7, 8, or 9?
    # Typo? if($Phonenumber=!/^[6-9]+\d/) # Fixed, but... if($Phonenumber !~ /^[6-9]+\d/)