Contributed by rodry
on Apr 17, 2000 at 00:39 UTC
Q&A
> CGI programming
Description: 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 CGI.pm (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 CGI.pm (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...
Russ | Answer: How do I detect and handle empty form fields? contributed by btrott Are you using CGI.pm? 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 CGI.pm
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!
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
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|