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.


In reply to Re: Empty form fields error message by jarich
in thread Empty form fields error message by n4mation

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.