Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

How do I detect and handle empty form fields?

by rodry (Beadle)
on Apr 17, 2000 at 00:39 UTC ( #7776=categorized question: print w/replies, xml ) Need Help??
Contributed by rodry on Apr 17, 2000 at 00:39 UTC
Q&A  > CGI programming


I wrote a script to validate the input of a form. All I need is for the script to check if the fields are not empty. Then, give the user a message prompting them to fill the information required.

Since there are more than ten fields in the form, I decided it would be quicker to store the variables in an array and then iterate thru them. Sort of like this:

@variablearray = \($variable1, $variable2, ....); foreach (@variablearray) { if (!$_){ $fieldempty = 1; } }
Is there a better way?

Answer: How do I detect and handle empty form fields?
contributed by chromatic

If you're using (and you should), you can check to see if a field is empty with the following code:

if (defined $q->param("this_is_your_field_name")) { # do something spectacular } else { $fieldempty = 1; # fail spectacularly }
Since most code that doesn't use (and you should be using it) at least puts fields in a hash, you can store your fieldnames in an array and still do right:
my @fieldnames = qw( name rank serialnumber haircolor favoritebeverage + ); foreach my $field (@fieldnames) { $fieldempty = 1 unless (defined $fields{$field}); } # do something spectacular here unless a field is empty
Answer: How do I detect and handle empty form fields?
contributed by Russ

my @InvalidFields = grep {not defined CGI::param($_) or CGI::param($_) eq ''} CGI::param +();

@InvalidFields will now contain the names of any empty fields.

Evaluate @InvalidFields in a scalar context to see how many empty fields there are.
if (@InvalidFields){ # You have some empty fields, so do something about them }

This general technique will work even if you are not using CGI...

Answer: How do I detect and handle empty form fields?
contributed by btrott

Are you using If so, you could use something like this:

use CGI; my $query = new CGI; my @fields = qw/name phone email/; for my $field (@fields) { print "$field is not defined" unless defined $query->param($field); }
@fields should hold the fields that you want to validate. Does that make sense?

Answer: How do I detect and handle empty form fields?
contributed by markjugg

The HTML::FormValidator module is designed to help you check for required fields, as well as validate the form input in a number of ways.

Answer: How do I detect and handle empty form fields?
contributed by Zombie Greg Harper

This is how i have been dealing with empty form fields:

# If certain fields are blank, then give a "blank form" response &missing_info_error ("Date") unless $FORM{'date'}; &missing_info_error ("Name") unless $FORM{'name'}; &missing_info_error ("Eng/Rig") unless $FORM{'engorrig'}; &missing_info_error ("Engine Model") unless $FORM{'em'}; &missing_info_error ("Test Stand or Build Cell") unless $FORM{'tsbc'}; &missing_info_error ("Time Lost") unless $FORM{'tl'};

Then output is in a HTML page

Update:mandog raised the issue that this approach fails if a legitimate field value is zero. In such cases, a missing field error is posted, even though the field exists, thus reinforcing the case for using

Edited by davido: Added code tags. Removed duplicate submission. Directed readers to further clarification.

Please (register and) log in if you wish to add an answer

  • 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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (4)
    As of 2020-08-03 13:01 GMT
    Find Nodes?
      Voting Booth?
      Which rocket would you take to Mars?

      Results (27 votes). Check out past polls.