Additional Required Fields With Master Feedback
Master Feedback from /msmf requires the
user's name and email address for successful submission of
the form.
Either of two modifications can be made to the program for
it to require additional fields to be filled in. Those two
methods can be classed as the simple and the versatile.
The simple modification is adding a line to the script for
each required field, hard coding the required field name.
The versatile modification is adding a block of code to
the script to read a hidden field for a list of required
field names.
The line numbers in the instructions below may vary
depending on which version you are using. These line
numbers are of the latest version, 2.73 completed
March 8, 2004.
If you're using an earlier version, please upgrade.
The latest has anti-form hijacking code. Depending on
which version you are using, other features may also
have been added since.
As always, edit Perl scripts with a plain text editor
like NotePad or BBEdit. WYSIWYG editors, rich text word
processors, and even some page makers, tend to insert
script-breaking formatting characters.
Note: A license to use Master Feedback is free of charge.
Pick up your copy at /msmf
The Simple Modification
At lines 112 through 115, you'll see four lines that each
begin with:
ErrorHTML(
At the top, at the bottom, or somewhere within the group of
lines, insert this line (underscores addressed below):
ErrorHTML('__________') unless $In{__________} =~ /\w/;
Replace the first underscore, the one between the single
quote charactes, with the message you want the user to see
when the form field is not filled in. If you use a single
quote character within the message itself, precede it with
a backward slash character. Example:
ErrorHTML('Harry\'s field must be filled in.');
Replace the second underscore, the one between the curly
braces, with the name of the field that is required. This
is case sensitive. If the field is name="BigBob", then
it must be
$In{BigBob}
One other consideration. If the field name contains spaces
or other non-alphanumeric characters, then enclose the name
in quotes. For example, name="Big Bob" would be
$In{"Big Bob"}
That's the whole simple process insert one line into the
script for every additional required form field.
The Versatile Modification
With this modification, you'll be adding a block of code
to read the value of a hidden field name="required" for a
list of required field names.
At lines 112 through 115, you'll see four lines that each
begin with:
ErrorHTML(
Lines 112 and 115 pertain to the user's name. Lines 113 and
114 pertain to the user's email address.
You may decide to keep those lines, or not. If you keep
them, the program will always make those fields required.
If you don't keep them, you'll need to list the field names
in your hidden field name="required" whenever you want them
to be required.
If you decide to remove the user's name requirement, also
remove line 111, which is
$In{realname} = $In{name} if $In{name};
That line is in the script for compatibility with earlier
versions of the form. It creates a field "realname" with
the value of field "name" if the form used a name="name"
field.
At the top, at the bottom, somewhere within the group
of lines, or in place of the lines (if you removed them),
insert the following 20 lines. The two beginning/end
comment lines are optional.
# beginning of insert
sub CommaSeparatedList
{
my $s = shift;
$s =~ s/^[\s\,]*(.*?)[\s\,]*$/$1/s;
$s =~ s/\s+,/\,/gs;
$s =~ s/,\s+/\,/gs;
return split /\,+/,$s;
}
if($In{required} =~ /\w/)
{
my @required = CommaSeparatedList $In{required};
my @e = ();
for(@required)
{
push @e,"Field $_ is required" unless $In{$_}=~/\w/;
}
ErrorHTML @e if @e;
}
# end of insert
No other modifications need to be done to the script.
To use the new feature, put a hidden field name="required"
into your forms. The value of the hidden field is a list
of required fields, each field name separated with a comma.
Example:
<input
type="hidden"
name="required"
value="field1,field2,field3" />
The code you inserted into the script automatically removes
spaces that occur before and after the commas. And when it
finds consecutive commas, it removes the extra ones. Thus,
value="field1 , field2,, ,, field3"
would be seen as identical to the previous example. In fact,
it also removes spaces and commas before and after the list,
and removes line breaks. Therefore, the following will also
be seen as identical to the above:
value=" ,,, field1 ,
field2
, field3 "
It may be unusual to inadvertently make a mistake with the
value, unless you mistype a field name. Remember that form
field names are case-sensitive.
That's the whole versatile process insert the provided
code into the script. Then use hidden field name="required"
to specify required fields.
Question:
Did you find this article interesting and understandable? How can it be improved?
Your response is anonymous.
When done typing, click anywhere outside the box. [more info]
Will Bontrager
©2005 Bontrager Connection, LLC
Please note:
Articles on this website are presented "as is". However -
If you have a question about a CGI script, HTML, CSS, PHP, or JavaScript
Ask one of our Experts and you'll have your answer!
Click here for details.