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

Hi monks,

I am new to cgi programming. I have to validate the form in CGI. How to validate the form using javascript code or cgi code. I use this below code to check in form whether the employee code is entered or not in form. The below mentioned code is not validating. so, any one plz. help for below mentioned code.
Thanks in advance.

#!C:\Perl5.8.4\bin\perl5.8.4.exe use CGI qw(:standard); print header; print start_html(-title=>"hello world", -bgcolor=>"#BOE0E6"); print end_html; print h1("<center>Hello World!</center>"); print qq{<form name="hello" action="firstdb.cgi" method="POST" onsubmi +t="return newsoft();"> Employee Code&nbsp;: <input type="text" name="empcode" size="20"><br>< +br> <input type="submit" value="Submit"> <input type="reset" value="reset"> }; print qq{<SCRIPT TYPE="javascript"> function newsoft() { if(isBlank(document.hello.empcode.value)==true) { alert("Please enter the Software"); document.hello.empcode.focus(); return false; } return true; } </SCRIPT>};

Replies are listed 'Best First'.
Re: How to validate the form in CGI
by Joost (Canon) on Mar 08, 2005 at 11:17 UTC
    isBlank() is not a predefined function in javascript. With firefox / mozilla you can open the javascript console with the url javascript: and you usually get decent error messages (at least, a lot better than in IE).

    Also, it's not realy safe to rely on javascript alone to validate a form. You can read and test input with the CGI module's param() method.

    update: and you should print the end_html() at the end of the html, not immediately after the start. :-)

      The mantra around the monastery is always "don't trust Javascript." That is true if a server-side check is omitted altogether, but Javascript can provide a very fast preliminary client-side check that doesn't require the long trip to the server. The JS validation protects the user with a fast check, while the server side protects the internals.

      I think both can work in tandem, with the understanding that the server-side validation will always be there.


      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
Re: How to validate the form in CGI
by Nevtlathiel (Friar) on Mar 08, 2005 at 14:23 UTC
    As others have suggested, you might like to look at using CGI as well as javascript to validate the user's input. Since you're new to CGI, I would recommend reading Ovid's CGI Course particularly Chapter Three which is all about security, checking user input and making sure that the input you allow will not mean people can do evil things to you - the first rule of CGI programming is assume that all your users are evil! ;)

    The excellent book Beginning Perl also has an Introduction to CGI which doesn't mention javascript but is still pretty useful.

Re: How to validate the form in CGI
by jhourcle (Prior) on Mar 08, 2005 at 12:04 UTC

    I think the biggest problem is your invalid submit element:

    <input type="submit" value="Submit" onsubmit=">

    When you're making any sort of CGI program, especially one that uses JavaScript, you should do the following to debug:

    1. Connect to the CGI
    2. View the source.
    3. Look over the source for properly formed and valid HTML.
    4. Look over the enclosed JavaScript for proper validity. (or run it through a browser that will give good error messages on javascript problems)

    That being said, you can never depend on a user having javascript turned on. If there is some sort of validation going on in the form, you should also validate it in the form backend, to make sure that the values are what you expect. (it may be that someone has JS turned off, or someone may be malicious and connect directly to your backend to try to force bad data on you).

      Look over the source for properly formed and valid HTML.

      But don't just eyeball it. There are tools to test it for you that are far less likely to miss things.

        Good point -- I shouldn't assume that everyone's using an editor that does syntax highlighting and validation.

        Some of the browsers have syntax coloring builtin when you view source, and there are a few that have built-in validators (iCab comes to mind). I've personally reset my source viewer with my text editor (BBEdit, as I'm a mac user). For those who haven't been writing HTML for more than a decade (damned <br> tag -- should've just made <address> preserve line endings)

        Anyway, there is a validation add-on for Mozilla, and there are even some validators in CPAN, like HTML::Validator and HTML::Tidy

Re: How to validate the form in CGI
by b10m (Vicar) on Mar 08, 2005 at 11:33 UTC

    It might be overkill for you, but you might like CGI::FormBuilder.

    --
    b10m

    All code is usually tested, but rarely trusted.
      I've had good experience with this module. Clean, object-oriented design. It supports both client- and server- side validation, in tandem, although personally I only use the server-side stuff. A lot of work went into version 3, although I have not used this either, and it is pretty new.
Re: How to validate the form in CGI
by manav (Scribe) on Mar 08, 2005 at 11:13 UTC
    - onsubmit() is given inside the FORM tag....it returns true or false.....

    see this onsubmit event to properly code your JavaScript.

    Manav
Re: How to validate the form in CGI
by mkirank (Chaplain) on Mar 08, 2005 at 12:04 UTC
    Check out CGI Docs

    #!perl use CGI qw(:standard); use strict ; use warnings; my $query = new CGI; print $query->header; print start_html(-title=>"hello world", -bgcolor=>"#BOE0E6" ); my $js =<<JS; <SCRIPT Language="javascript"> function check_answer() { if(document.hello.empcode.value==false) { alert("Please enter the Software"); document.hello.empcode.focus(); return false; } return true; } </SCRIPT> JS print "$js \n\n"; print $query->startform(-method=>"POST",-action=>"mycgi.cgi",-onSubmit +=>'return check_answer()',-name=>'hello'); print $query->textfield(-name=>'empcode', -size=>20, -maxlength=>20); print $query->submit(-name=>'submit', -value=>'submit'); print $query->reset ; print $query->endform;
Re: How to validate the form in CGI
by markjugg (Curate) on Mar 08, 2005 at 22:42 UTC
Re: How to validate the form in CGI
by metaperl (Curate) on Mar 08, 2005 at 17:36 UTC
Re: How to validate the form in CGI
by Popcorn Dave (Abbot) on Mar 08, 2005 at 16:51 UTC
    I'm suprised nobody mentioned this, but you really should be untainting your data in addition to checking that it exists. That way if someone puts erroneous data in to your employee number, you'll catch it before it potentially causes any damage. Do a perldoc taint for further reading.

    And as other Monks have mentioned, you really should look at using the param method of CGI.pm. It makes life a lot easier when doing this type of thing.

    Useless trivia: In the 2004 Las Vegas phone book there are approximately 28 pages of ads for massage, but almost 200 for lawyers.
Re: How to validate the form in CGI
by kprasanna_79 (Hermit) on Mar 09, 2005 at 09:34 UTC
    You try out the following perl modules
    CGI::Application::ValidateRM;
    Data::FormValidator
    --prasanna.k