DigitalKitty has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl -wT use strict; use CGI; use CGI::Carp qw( fatalsToBrowser ); my $q = new CGI; my $colon = ":"; my $f_name = ""; my $f_name_error = ""; my $f_email_error = ""; my $f_mail_address = ""; my $f_mail = ""; my $f_mail_error= ""; my $f_city = ""; my $f_city_error = ""; my $f_state = ""; my $f_state_error = ""; my $f_zip = ""; my $f_zip_error = ""; my $f_share = ""; my $f_shareword = ""; my $f_email = ""; my $p_Terrors = 0; my $f_contact = ""; print "Content-type: text/html\n\n"; # If the first time the form is shown, then ... if ($q->param == 0) { &S_Show_Form(); exit(); } else { # If this is not the first time the form is shown. #1. Get the value for the control named f_name. $_ = $q->param("f_name"); s/^[\s]*//; #Remove leading blanks. s/[\s]*$//; #Remove trailng blanks. $f_name = $_; $p_Terrors = 0; #3. Check each form field for errors. if ($f_name eq "") { $f_name_error = "<font color=\"red\">Name may not be blank.</f +ont><br>\n"; $p_Terrors = $p_Terrors + 1; } $_ = $q->param("f_email"); s/^[\s]*//; #Remove leading blanks. s/[\s]*$//; #Remove trailng blanks. $f_email = $_; if ($f_email eq "") { $f_email_error = "<font color=\"red\">Email may not be bla +nk.</font><br>\n"; $p_Terrors = $p_Terrors + 1; + } + $_ = $q->param("f_mail_address"); s/^[\s]*//; #Remove leading blanks. s/[\s]*$//; #Remove trailng blanks. $f_mail_address = $_; if ($f_mail_address eq "") { $f_mail_error = "<font color=\"red\">Address may not be bl +ank.</font><br>\n"; $p_Terrors = $p_Terrors + 1; + } + $_ = $q->param("f_city"); s/^[\s]*//; #Remove leading blanks. s/[\s]*$//; #Remove trailng blanks. $f_city = $_; if ($f_city eq "") { $f_city_error = "<font color=\"red\">City may not be blank.</fo +nt><br>\n"; $p_Terrors = $p_Terrors + 1; + } $_ = $q->param("f_state"); s/^[\s]*//; #Remove leading blanks. s/[\s]*$//; #Remove trailng blanks. $f_state = $_; if ($f_state eq "") { $f_state_error = "<font color=\"red\">State may not be blank.</ +font><BR>\n"; $p_Terrors = $p_Terrors + 1; + } $_ = $q->param("f_zip"); s/^[\s]*//; #Remove leading blanks. s/[\s]*$//; #Remove trailng blanks. $f_zip = $_; unless ( $f_zip =~ /\d{5}/ ) { $f_zip_error = "<font color=\"red\">Zip code cannot be blan +k and must be 5 digits in length.</font><BR>\n"; $p_Terrors = $p_Terrors + 1; } #4. When all done checking individual form fields for errors, s +ee if the count of errors = zero. if ($p_Terrors == 0) { #No errors. Call subroutine "S_Process_Form" &S_Process_Form(); } else { #We had one or more errors, Show the form again. &S_Show_Form(); } } #End if $q == 0 exit(); #--------------- sub S_Show_Form { #--------------- print "<HTML>\n"; print "<TITLE>WEB SITE REGISTRATION</title>\n"; print "</head>\n"; print "<BODY BGCOLOR=\"cornsilk\">\n"; print "<FONT COLOR=\"cornflowerblue\"><B>\n"; print "<CENTER>\n"; print "<H1><b><FONT COLOR=\"crimson\">REGISTER WEB SITE</font></b> +</h1>\n"; print "<BR>\n"; print $q->start_form(-action => "customer.cgi", method => "get"); #The next print statment will print "" (nothing) if # there was no error in registering person's name. # Otherwise it will print the error message. print $f_name_error; print "Name: ", $q->textfield(-name=>"f_name", -size=>30), "<BR>\n +"; print $f_email_error; print "E-mail address: ", $q->textfield(-name=>"f_email", -siz +e=>30), "<BR>\n"; print "$f_mail_error"; print "Mailing address: ", $q->textfield(-name=>"f_mail_addres +s", -size=>30), "<BR>\n"; print "$f_city_error"; print "City: " . $q->textfield(-name=>"f_city", -size=>30), "<br>\ +n"; print "$f_state_error"; print "State: " . $q->popup_menu(-name=>"f_state", -value=>['P +lease select your state', 'Az', 'Ca', 'Hi'], -labels=>{'Az'=>'Arizona +', 'Ca'=>'California', 'Hi'=>'Hawaii'}), "<BR>\n"; print "$f_zip_error"; print "Zip: ", $q->textfield(-name=>"f_zip", -size => 15), "<B +R>\n"; print "Contact: ", $q->radio_group(-name=>"f_contact", -value= +>['Email', 'Postal mail'], -default=>'Email'), "<BR>\n"; print "Share with others?", $q->checkbox(-name=>'f_share', -va +lue=>'', -label=>''), "<BR>\n"; #The next two lines are sample for print the email address error ( +if any) #and the text field for the email address. print $q->submit(-value =>"Submit Information"); print "</b></font>\n"; print $q->end_form(); } #end-sub S_Show_Form #----------------- sub S_Process_Form { #----------------- #The test immediately below checks the value #of the "share/don't share" checkbox and sets #a variable named $Vshareword = "Y" or "N" --- #so that value can be written to the customer.txt file. if ($f_share eq "on") { $f_shareword = "Y"; } else { $f_shareword = "N"; } open(CUSTFILE, ">>customer.txt"); print CUSTFILE "$f_name" . "$colon" . "\n"; close(CUSTFILE); print "$f_name thank you for registering at our site.<BR>\n"; if ($f_share eq "on") { print "We appreciate you allowing us to share your information wit +h our sponsoring companies.<BR>\n"; } else { print "We acknowledge your request that your information NOT be sh +ared with others.<BR>\n"; } } #end sub ProcessForm
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl Form Validator problems.
by vladb (Vicar) on Apr 27, 2002 at 04:11 UTC | |
|
Re: Perl Form Validator problems.
by DaWolf (Curate) on Apr 27, 2002 at 06:18 UTC | |
by chromatic (Archbishop) on Apr 27, 2002 at 17:19 UTC | |
by DigitalKitty (Parson) on Apr 27, 2002 at 20:52 UTC | |
by DaWolf (Curate) on Apr 28, 2002 at 00:36 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |