Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

How do I detect and handle empty form fields?

by rodry (Beadle)
on Apr 17, 2000 at 00:39 UTC ( [id://7776]=perlquestion: print w/replies, xml ) Need Help??

rodry has asked for the wisdom of the Perl Monks concerning the following question:

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?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
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
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
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?

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.
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.

Re: How do I detect and handle empty form fields?
by Anonymous Monk on Mar 15, 2002 at 07:07 UTC
    All the suggestions appear to be valid - except the one using JavaScript. DO NOT USE CLIENT SIDE VALIDATION AS YOUR ONLY METHOD OF VALIDATION. You put the server at risk, as well as files and databases when you do not validate on the server-side. JavaScript can be turned off on a browser - and is NOT used for text-based browsers like Lynx. By all means, use JavaScript to validate IF you are also validating on the server-side from within the script.

    Originally posted as a Categorized Answer.

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.

Re: How do I detect and handle empty form fields?
by Anonymous Monk on Oct 11, 2004 at 06:32 UTC
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <script language="JavaScript" type="text/javascript"> //function to check empty fields function isEmpty(textfield1, textfield2, textfield3) { //change "field1, field2 and field3" to your field names textfield1 = document.forms1.field1.value textfield2 = document.forms1.field2.value textfield3 = document.forms1.field3.value //name field if (textfield1 == "" || textfield1 == null || !isNaN(textfield1) || textfield1.charAt(0) == ' ') { alert("\"Field 1\" is a mandatory field.\nPlease amend and retry.") return false; } //url field if (textfield2 == "" || textfield2 == null || textfield2.charAt(0) == ' ') { alert("\"Field 2\" is a mandatory field.\nPlease amend and retry.") return false; } //title field if (textfield3 == "" || textfield3 == null || textfield3.charAt(0) == ' ') { alert("\"Field 3\" is a mandatory field.\nPlease amend and retry.") return false; } return true; } </script> <body> <form name="form1" method="post" action="">
    Name : <input type="text" name="textfield">
    Address : <input type="text" name="textfield2">
    Phone : <input type="text" name="textfield3">
    E-mail : <input type="text" name="textfield4">
    <input type="submit" name="Submit" value="Submit"> <script language="JavaScript" type="text/javascript">
    <input type="reset" name="Submit2" value="Reset">
    </form> </body> </html>

    Originally posted as a Categorized Answer.

Re: How do I detect and handle empty form fields?
by Greg Harper (Initiate) on Oct 02, 2003 at 18:00 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'};

    Originally posted as a Categorized Answer.

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.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://7776]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-03-29 10:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found