in reply to Empty form fields error message

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.

Replies are listed 'Best First'.
Re: Re: Empty form fields error message
by TomK32 (Monk) on Nov 25, 2001 at 07:53 UTC
    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.
Re: Re: Empty form fields error message
by n4mation (Acolyte) on Nov 26, 2001 at 01:45 UTC
    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.