Use of Java script to validate form fields in Perl

Discussion in 'Perl Misc' started by Kuna, Dec 27, 2006.

  1. Kuna

    Kuna Guest

    Hi All,

    I am trying to use Java script to validate the form fields. The code is
    written in Perl and it is having a .pm extension for this form also
    there is a index.pl file which is containing the "Save" button and the
    form is posting the data to index.pl file and then it again comes to
    the methods written in the .pm file which is validating the form using
    server side code and submit that. But I am not able to get that where
    to do the Java script validation that will validate the form in the
    client side. Here I am having some codes that will give some idea about
    my form.

    <code>
    print "<body>
    <table width='100%' border='0' cellspacing='0' cellpadding='0'>
    <tr>
    <td width='22%' height='18'><img src='./images/logo_mmna.jpg'
    width='227' height='57'></td>
    <td width=''3%'>&nbsp;</td>
    <td width='49%'>&nbsp;&nbsp;&nbsp;&nbsp;
    <div align='center' class='v10b'><span class='style3'>"
    . $self->getText("NOTICE OF DESIGN PROCESS CHANGE") .
    "</span><br>
    </div></td>
    <td width='26%' valign='top'><table width='100%' border='0'
    cellspacing='0' cellpadding='0'>
    <!--DWLayoutTable-->
    <tr>
    <td height='18' colspan='3'>&nbsp;</td>
    </tr>
    <tr>
    <td width='20%' height='18' class='v10b'><label>"
    . $self->getText("Date:") . " </label></td>
    <td width='17%'>";
    print hidden(
    -name => '.formId',
    -value => '4MChange',
    -tabindex => '-1',
    -override => 1
    );
    print textfield(
    -name => 'txt-date',
    -value => $self->retSysTime,
    -id => 'txt-date',
    -class => 'formelements',
    -size => '10',
    -title => 'Today\'s date',
    -tabindex => $tabindex
    ).'*';

    foreach my $thisLabel (@singleEntryFields) {
    $result->{$thisLabel} = textfield(
    -name => sprintf( "txt-%s", $thisLabel ),
    -value => $data{$thisLabel},
    -id => sprintf( "txt-%s", $thisLabel ),
    -class => ( $errLabel eq $thisLabel )
    ? 'formmsg'
    : 'formelements',
    -size => $sizeHash{$thisLabel},
    -title => $titleHash{$thisLabel},
    -force => 1
    );
    }

    print "<td>
    <td height=\"10\" colspan=\"3\"><img src=\"/images/spacer.gif\"
    width=\"5\" height=\"8\"></td>
    </tr>
    <tr>
    <td height=\"18\"></td>
    <td colspan=\"2\">&nbsp;</td>
    </tr>
    </table></td>
    </tr>
    <tr>
    <td height=\"18\" colspan=\"4\">&nbsp;</td>
    </tr>
    <tr valign=\"top\">
    <td height=\"14\" colspan=\"4\"><div align=\"left\"
    class=\"v10b\"><label>"
    . $self->getText('* indicates a field is required')
    . "</label></div>";
    print p();
    print
    "<table width=\"100%\" border=\"0\" cellpadding=\"0\"
    cellspacing=\"0\" bgcolor=\"#E9E9E9\">
    <tr>
    <td width=\"35%\" valign=\"top\"><table width=\"100%\"
    border=\"0\" align=\"left\" cellpadding=\"0\" cellspacing=\"0\">
    <tr>
    <td width=\"22%\"><div align=\"right\"
    class=\"v10b\"><label>"
    . $self->getText("Supplier Name") . "</label>:&nbsp; </div></td>
    <td width=\"20%\">";
    $tabindex++;
    print $result->{'sname'}.'*';
    print "</td>
    </tr>
    <tr>
    <td colspan='2'><img src='/images/spacer.gif' width='5'
    height='8'></td>
    </tr>
    <tr>
    <td><div align='right' class='v10b'><label>"
    . $self->getText("Supplier Code:") . "</label>&nbsp;</div></td>
    <td align ='left'>";
    $tabindex++;

    print $result->{'scode'}.'*';
    print "</td>
    </tr>
    </table></td>
    <td width='35%' valign=\"top\">
    <table width='100%' border='0' align='left' cellpadding='0'
    cellspacing='0'>
    <tr>
    <td width='28%'><div align='right' class='v10b'><label>"
    . $self->getText("Supplier Contact:") . "&nbsp;</label>
    </div></td>
    <td width='14%'>";
    $tabindex++;

    print $result->{'scontact'}.'*';
    print "</td>
    </tr>
    <tr>
    <td colspan='2'><img src='/images/spacer.gif' width='5'
    height='8'></td>
    </tr>
    <tr>
    <td><div align='right' class='v10b'><label>"
    . $self->getText("Supplier Fax:") . "&nbsp;</label></div></td>
    <td align='left'>";
    $tabindex++;

    print $result->{'sfax'}.'*';
    print "</td>
    </tr>
    </table></td>
    <td width='30%' valign=\"top\">
    </code>

    This the form in the .pm file and in the index.pl is having the button
    as below:

    <code>
    if ( param() ) {

    $act = $cgi->param('act') || "preprocess";
    $button = $cgi->param('button') || 'Save';
    $no = ( defined $cgi->param('.no') ) ? ( $cgi->param('.no') ) :
    $defaultNo;
    $tloc = $cgi->param('.tloc');
    $tsvData = $cgi->param('data');
    my $error;
    $formId =
    ( defined $cgi->param('.formId') ) ? $cgi->param('.formId') :
    $formId;
    my $ev = sprintf
    'use ACI::WebForms::Form%s; $thisform = new
    ACI::WebForms::Form%s();',
    $formId, $formId;
    if ( !eval $ev ) {
    die $@;
    }
    <./code>

    So what I am not able to get that since I am new to Perl, so I am not
    getting any <form> tag where I can use the Java script call. Also I am
    not getting any solution where to add the Java script that will take
    the form input data and validate it.

    Sorry if I had missed any information....Please look into this I need a
    urgent help.......

    Thanks and Regards,
    Kuna
     
    Kuna, Dec 27, 2006
    #1
    1. Advertising

  2. Kuna <> wrote:

    > I am trying to use Java script



    I doubt that.

    I expect you are trying to use JavaScript.


    > to validate the form fields. The code is
    > written in Perl and it is having a .pm extension for this form also
    > there is a index.pl file which is containing the "Save" button and the
    > form is posting the data to index.pl file and then it again comes to
    > the methods written in the .pm file which is validating the form using
    > server side code and submit that. But I am not able to get that where
    > to do the Java script validation that will validate the form in the
    > client side.



    You must rewrite the .pm routines in JavaScript, then from Perl
    you print() the JavaScript code at the appropriate place.


    > -name => sprintf( "txt-%s", $thisLabel ),



    It is an abuse to use s(tring)printf(ormated) when you are not
    applying any formatting.

    -name => "txt-$thisLabel",


    > $formId =
    > ( defined $cgi->param('.formId') ) ? $cgi->param('.formId') :
    > $formId;



    That is a strange way to say:

    $formId = $cgi->param('.formId') if defined $cgi->param('.formId');


    > my $ev = sprintf
    > 'use ACI::WebForms::Form%s; $thisform = new
    > ACI::WebForms::Form%s();',
    > $formId, $formId;



    If the module is designed to be used this way, then it is very
    poorly designed.


    > if ( !eval $ev ) {
    > die $@;
    > }



    Using that form of eval() is very very dangerous!

    You had better add some taint checking (perldoc perlsec) or
    prepare to be hacked.


    > So what I am not able to get that since I am new to Perl,



    The only piece of Perl knowledge that you need to accomplish this
    is how to use the print() function.

    You need JavaScript knowledge to know what you want to print().


    > Please look into this I need a
    > urgent help.......



    Pleading is counter-productive.

    Have you seen the Posting Guidelines that are posted here frequently?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Dec 27, 2006
    #2
    1. Advertising

  3. Tad McClellan wrote:
    > Kuna <> wrote:
    >
    >> I am trying to use Java script

    >
    >
    > I doubt that.
    >
    > I expect you are trying to use JavaScript.
    >
    >
    >> to validate the form fields. The code is
    >> written in Perl and it is having a .pm extension for this form also
    >> there is a index.pl file which is containing the "Save" button and the
    >> form is posting the data to index.pl file and then it again comes to
    >> the methods written in the .pm file which is validating the form using
    >> server side code and submit that. But I am not able to get that where
    >> to do the Java script validation that will validate the form in the
    >> client side.

    >
    >
    > You must rewrite the .pm routines in JavaScript, then from Perl
    > you print() the JavaScript code at the appropriate place.


    As an addendum to this, CGI::FormBuilder can be used to do a lot of the
    heavy lifting. It will, among other things, generate HTML forms and the
    Javascript code necessary for client-side validation. The learning curve
    may be a little steep, however.

    Mark
     
    Mark Clements, Dec 27, 2006
    #3
  4. Kuna

    Ted Zlatanov Guest

    On 27 Dec 2006, wrote:

    Tad McClellan wrote:
    > Kuna <> wrote:
    >
    >>> I am trying to use Java script


    >> I doubt that.
    >>
    >> I expect you are trying to use JavaScript.
    >>
    >>
    >>> to validate the form fields. The code is
    >>> written in Perl and it is having a .pm extension for this form also
    >>> there is a index.pl file which is containing the "Save" button and the
    >>> form is posting the data to index.pl file and then it again comes to
    >>> the methods written in the .pm file which is validating the form using
    >>> server side code and submit that. But I am not able to get that where
    >>> to do the Java script validation that will validate the form in the
    >>> client side.

    >>
    >>
    >> You must rewrite the .pm routines in JavaScript, then from Perl
    >> you print() the JavaScript code at the appropriate place.

    >
    > As an addendum to this, CGI::FormBuilder can be used to do a lot of
    > the heavy lifting. It will, among other things, generate HTML forms
    > and the Javascript code necessary for client-side validation. The
    > learning curve may be a little steep, however.


    I should mention for beginners' benefit that Javascript validation is
    nothing but a courtesy to the user. If you don't validate the data on
    the server, you are asking for trouble. Anyone can submit form data
    directly, bypassing the Javascript checks. So the server needs to
    reply with the same form, with the submitted valid values filled in,
    and the invalid ones marked. It's annoying, and I would agree with
    Mark Clements that CGI::FormBuilder does much of this and should be
    the first choice in most simple situations. Unfortunately some
    database setups may need dedicated code for form validation, to ensure
    atomic operations for example (so validation and insertion happen at
    the same time, which is not the CGI::FormBuilder model).

    Ted
     
    Ted Zlatanov, Dec 27, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. morc

    Validate Form Fields JSP

    morc, Feb 1, 2006, in forum: Java
    Replies:
    1
    Views:
    695
    Hal Rosser
    Feb 2, 2006
  2. Neo Geshel
    Replies:
    2
    Views:
    526
    Neo Geshel
    Feb 28, 2007
  3. Colin Graham

    Javascript Validate Form Fields

    Colin Graham, Jan 25, 2005, in forum: Javascript
    Replies:
    1
    Views:
    115
  4. Meredith
    Replies:
    1
    Views:
    127
    Meredith
    Feb 11, 2005
  5. phpCodeHead
    Replies:
    5
    Views:
    151
    Dr J R Stockton
    Apr 5, 2007
Loading...

Share This Page