in reply to Re: Create required fields from form
in thread Create required fields from form

Hi,

That's what I would like to do. I'm just not sure who to go about writing the code to accomplish this.

Any assistance would be great!
  • Comment on Re: Re: Create required fields from form

Replies are listed 'Best First'.
Re: Re: Re: Create required fields from form
by CountZero (Bishop) on Jan 23, 2004 at 20:54 UTC
    OK, we'll try.

    Some questions first:

    • Is your form a static web-page or do you dynamically make it with a (Perl-)script?
    • How do you process the results of your form? Do you use CGI.pm?

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      Hi,

      My form is a static web-page which I created with FrontPage.

      The result is being processed with CGI.pm and Perl.

      I hope that helps.

      Thanks for helping me out.
        You could use the following as a skeleton for your script. The script both creates the page with the form (subroutine formmaker - the content of which you should replace with your webpage of course) and evaluates the returned parameters, if necessary re-displaying the form with an appropriate message to fill in the email-addres.

        Checking whether the email is 'valid' is left as an excercise for the reader. The regex used should not even be considered to be the basis of such test.

        use strict; use CGI qw/:standard/; if (param()) { if (param('email')=~/\w+@\w+/) { print header, start_html('Thank you'), h1('Thank you'), p, param('name'), ' now that we have your e-mail address w +e will send lots of nice spam to you at ', param('email'), '!', p; } else { formmaker('You must provide an email address!'); } } else { formmaker(); } sub formmaker { print header, start_html('A Form with Sticky Fields'), h1('A Form with Sticky Fields'), p; my $message = shift; print $message, p if $message; print start_form, "What's your name? ",textfield('name'),p, "What's your email? ",textfield('email'), p, ,p, submit, end_form, hr; }

        As you will see when you test this script, the fields are 'sticky', i.e. they keep their values between invocations.

        CountZero

        "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: Re: Re: Create required fields from form
by Vautrin (Hermit) on Jan 23, 2004 at 21:01 UTC

    The best way to check whether or not an e-mail address is valid is to make sure it is of a valid format. You can do this using regular expressions:

    use strict; use warnings; use CGI; my $CGI_obj = CGI->new; my $email = $CGI_obj->param('email'); # or is it params? if ($email =~ m/.*?@.*?\..*?/) { print "This e-mail is valid\n"; } else { print "This is not a valid e-mail\n"; }

    You really should check out one of the regular expressions tutorials, i.e.: perlretut Remember that PerlDoc.com and Google are your friends.

    Now, you might be saying to yourself, "But how do I know that the e-mail address will work?". The only real way to do that is to send an e-mail to the address you're checking.

    The reasons for this are mostly related to efforts at spam prevention. There is some command you can send to port 25 (the email port) if you open up a socket to "ping" it and see if it accepts emails. However, because many networks dont want spammers looking for their e-mail addresses they may disable it. You can look up a domain name to see if you can get an IP address, but this doesn't always work for exotic domains unless your script is very robust.

    You can even open up port 25 to send email, and just never complete it, but, again, for spam related purposes if you're not on the network the server isn't guaranteed to respond, even with a "No relaying allowed" message. The other thing to look out for is the email address may be valid but not the original users. So they may be redirecting it to a cache all e-mail (some servers forward all email to root@localhost or webmaster@localhost if the user is not valid). Or they may never read their e-mail.

    So, stick with regular expressions. Or you can send out an e-mail to the user asking them to confirm their submission. However, that is a whole nother can of worms and suitable for another thread.

    Best Regards,

    Vautrin
      When I make forms of all kinds (which is all the time since I only program under CGI), I test required fields using if/else iterations. Ie.
      if (param('submit')) { my $name = param('name'); my $email = param('email'); my $message = param('message'); if($name ne "") { print "Good, you have a name!\n"; } else { print "Hey buddy, I know you have a name!\n"; } if($email ne "") { print "Alright, I know your email addy\n"; } else { print "No email addy from you? No email to me!\n"; } if($message ne "") { print "Thanks for your message\n"; } else { print "Excuse me? I didn't hear you say anything!!\n"; }
      You are testing each field against "" (or nothing). If you want, you can add an exit; command after it finds a blank field like I usually do.

      Hope this helps.



      "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

      sulfericacid
        Hi,

        Yes, you gave me great information! Thanks!

        In that case when the field is blank, how can I redisplay a static web page form with a message for people to fill in the blank field?

        Thanks!
      Hi,
      <br I read the documentation in perlretut as you suggested. The documentation talks about matches. I'm not trying to match email addresses to anything.

      All I'm doing is having people complete and submit the form. The email address is required. If a person doesn't enter the email address, bring up a message to fill in the field, and present the form with its contents intact.