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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Empty form fields error message
by jarich (Curate) on Nov 25, 2001 at 07:43 UTC
    I suspect that you're looking for javascript here. If you want a little popup box that says something like "You can't submit this form without fulling in the name field" etc, then that's javascript.

    There's an excellent javascript site with hundreds of examples to do that kind of thing at http://javascript.internet.com.

    If instead you want to do form validation after the user has submitted then HTML::FormValidator as suggested by rob_au is a good bet.

    Alternately you can do something like the following:

    use strict; use CGI; my $cgi=CGI->new; print $cgi->header; my @un_filled; foreach ($cgi->param) { unless($cgi->param($_)) { push @un_filled, $_; } } if(@un_filled) { print "You've forgotten to fill in the following fields: @un_filled"; exit; } # do stuff...
    Calling param() in a list context with no arguments returns all of the parameter names passed to it.

    Good luck.

    Update: thanks to TomK32 for reminding me. You never want to rely on Javascript to have validated your forms. It's nice as a quick reminder to the user that a field should be filled in, but nothing more than that.

    Ideally your script should handle proper validation as a matter of course. Checking that all the fields you expected have been passed back, that the fields contain valid entries (numbers for years, letters for names etc). Javascript is really just a convenience thing.

    Doing taint checking before using these values is also recommended. Look at perldoc perlsec.

      Using Javascript (JS) for checking a field is no good idea.

      • The user might not have JS activated
      • You can go around the JS
      • Servers are more powerful than they were 1999
      Update: OK, for slow connections client-side checking is a good idea, as long as you keep the server-side checking. But hey, why do a job twice?
        Imho JS checks should be added to make it more user friendly. Especially for users that have a dialup or slow connection. If JS is not supported by the browser the user experience just degrades a little.
        In any case, the server has to do the checking too. This is not only because the browser may not support JS, but it is possible for a malicious person to take your html page and submit the doctored version.
      Thank you monks! Works now! It wuz very simple once I understood I could call the param without agruments. I'm new to Perl, and I'm embarassed to show you my bloated script.
Re: Empty form fields error message
by rob_au (Abbot) on Nov 25, 2001 at 07:10 UTC
    Given the lack of context in your question, there isn't a great deal which I can offer in useful reply. Personally I would direct you to have a look at HTML::FormValidator which offers you the ability not only to display messages when mandatory fields are left null, but also when values in key fields do not pass validation checks.

    I would also direct you to heed the excellent advice given to you by demerphq here in reply to your previous question with regard to how to get the most out of your questions asked here on PM.

     

    Ooohhh, Rob no beer function well without!

Re: Empty form fields error message
by cLive ;-) (Prior) on Nov 25, 2001 at 08:56 UTC

    Here's another way. Add a comment - <--error_message--> - to the form page where you want the error message to display, then use something like this:

    #!/usr/bin/perl -w use strict; use CGI; # create CGI object my $q = new CGI; # create list of fields to check my @non_empty_form_fields = qw(name email address tel); # store errors here my @err = (); # check fields exist for (@non_empty_form_fields) { unless ( $q->param($_) ) { push @err, "You must complete the '$_' field"; } } # if error, represent if (@err) { # read in form open (FORM,'/path/to/form.html') || die $!; my $form = join '', (<FORM>); close(FORM); # create error message my $err_message = $q->h3('Error'). $q->p('I could not process the form:'). $q->ul( $q->li([@err]) ). $q->p('Check your input and resubmit.'); # put message in page $form =~ s/<!--error_message-->/$err_message/is; # show page print $q->header, $form; exit(0); } # otherwise input ok, so do whatever...

    If you are using relative URLs to call page images, you may also want to add a <BASE HREF...> tag to the page (or get the script to add it).

    HTH

    cLive ;-)

Re: Empty form fields error message
by nlafferty (Scribe) on Nov 26, 2001 at 02:10 UTC
    Just check the variable for data.
    #!/usr/bin/perl use CGI qw/:standard/; print header; $form_field = param("form_field"); # get the value from form if ($form_field eq ""){ # if value is nothing &print_error; # run subroutine }else{ # else print "$form_field\n"; # do your stuff } sub print_error{ # Error subroutine print "there was an error\n"; }