Make All Form Fields Required

They have: 40 posts

Joined: Oct 2001

Anyone know of a simple way to make every field in a form required? (not validated, just required)

Busy's picture

He has: 6,151 posts

Joined: May 2001

Any options for programing languages used, php, asp, perl ...?

They have: 601 posts

Joined: Nov 2001

Tell us what programming language you are using?

In Perl, you would either pre-define your vars, arrays or whatever data structure you are passing to your script and then do a simple test:

my @required = ($foo,$bar,@foobar...);

  foreach @required {
    if (!$_) {
      die ("Required field $_ left blank");
    }
  }
'
If you're lazy and you don't want to define all your required form fields before hand, use the query param function of CGI.pm to obtain the entire paramater list passed to the script.

  foreach ($query->param) {
    if (!$_) {
      die ("Required field $_ left blank");
    }
  }
'

Hope this helps.

- wil

They have: 40 posts

Joined: Oct 2001

Hey thanks Wil,

Your "lazy" method is definitely what I need, as there are over 100 fields in the form (it's for someone else, not me...I would never have such a huge form). And I am using perl.

I'm not completely up with CGI.pm, so I'll tell you what I put where, and then maybe you might tell me why it didn't work.

Up the top, below the shebang is use CGI; (I tried use CGI.pm;, but this gave an internal server error)
Then, after a few variables are defined, I have

$query = new CGI();
foreach ($query->param) {
if (!$_) {
die ("Required field $_ left blank");
}
}

Then, I have the code which gets the form values. I suspect this might be part of the problem, and please don't laugh too hard if the combined code is ridiculous. I'm looking forward to your help! Here it is:

if ($ENV{'QUERY_STRING'}) # using form->get method
{
@pairs = split(/\&/,$ENV{'QUERY_STRING'});
}
elsif ($ENV{'CONTENT_LENGTH'}) # using form->post method
{
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
}

undef @send;
undef @send_values;

foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/~!/ ~!/g;

push @send, $name;
push @send_values, $value;
$FORM{$name} = $value;
}

Anyway, at present, there are no errors, but when I submit the form with nothing filled in, it just goes through without giving me any error messages. I'm sure you know why...Thanks a lot.

They have: 601 posts

Joined: Nov 2001

Hi

I'm not laughing at all! Jeepers, I'm not an expert by a long shot! Well all have to start somewhere. You've done a good job, thus far. Let me try and give you some advice...

Regarding "use CGI" and "use CGI.pm". I'll tell you why you should use the first and not the last example. "pm" stands for "P"erl "M"odule. They are all stored in a special directory in the Perl distribution. Perl knows to go looking here for modules by default. You can also define more directories for Perl to go looking for modules. Because Perl knows that what you're trying to "use" is a perl module, and it knows where to look so it doesn't need an exact file name/extension. Therefore, you just omit the file extension, and type whatever is before the period.

OK. I've tried to re-write your script a little. What you're doing wrong is that you're checking for empty variables before the variables actually gets passed to the script. Therefore there is no such variables defined for them to be empty or not, so Perl doesn't do anything with that loop.

Because you're already using CGI.pm, I'll change some code for you as well. You don't need to split variables passed to your form yourself as CGI.pm does a good job of that for you too. Here's what I would write instead.

#!/usr/bin/perl

  use CGI;
  $query = CGI::new();

  # This imports everything passed to the script
  # into the name space 'q'.
 
  $query->import_names('q');

  if ($ENV{'QUERY_STRING'})
  {
    foreach $name ($query->param) {
    my $value = $query->param($name);
    if (!$value) {
      die ("Required field $name left blank"); 
    }
    else {

    # Here you can acccess all variable name/value pairs.
    # I might as well print them all for you.

    print "$name=$value\n";  
    }

    # Or you can access all your variables by using
    # $q::var_name or @q::array_name if it was an array.
    # This is handy if you know the variable name and you
    # need to access it's value.
  }
'

Hope this helps.

- wil

They have: 40 posts

Joined: Oct 2001

Thanks Wil,

I'll try this as soon as I can...And then let you know...

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.