Re: How do I detect and handle empty form fields?
by chromatic (Archbishop) on Apr 17, 2000 at 01:49 UTC
|
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
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: How do I detect and handle empty form fields?
by Russ (Deacon) on Jul 05, 2000 at 13:24 UTC
|
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 | [reply] [Watch: Dir/Any] [d/l] [select] |
Re: How do I detect and handle empty form fields?
by btrott (Parson) on Apr 17, 2000 at 04:09 UTC
|
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?
| [reply] [Watch: Dir/Any] [d/l] |
Re: How do I detect and handle empty form fields?
by markjugg (Curate) on Feb 06, 2001 at 01:48 UTC
|
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. | [reply] [Watch: Dir/Any] |
Re: How do I detect and handle empty form fields?
by Greg Harper (Initiate) on Oct 02, 2003 at 18:09 UTC
|
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.
| [reply] [Watch: Dir/Any] [d/l] |
|
| [reply] [Watch: Dir/Any] [d/l] |
Re: How do I detect and handle empty form fields?
by Anonymous Monk on Mar 15, 2002 at 07:07 UTC
|
| [reply] [Watch: Dir/Any] |
Re: How do I detect and handle empty form fields?
by tye (Sage) on Oct 22, 2001 at 21:34 UTC
|
Re: How do I detect and handle empty form fields?
Originally posted as a Categorized Answer. | [reply] [Watch: Dir/Any] |
Re: How do I detect and handle empty form fields?
by Anonymous Monk on Oct 11, 2004 at 06:32 UTC
|
| [reply] [Watch: Dir/Any] |
Re: How do I detect and handle empty form fields?
by Greg Harper (Initiate) on Oct 02, 2003 at 18:00 UTC
|
| [reply] [Watch: Dir/Any] |
Re: How do I detect and handle empty form fields?
by Anonymous Monk on Oct 22, 2001 at 10:46 UTC
|
Re: How do I detect and handle empty form fields?
Originally posted as a Categorized Answer. | [reply] [Watch: Dir/Any] |
A reply falls below the community's threshold of quality. You may see it by logging in. |