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

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

Replies are listed 'Best First'.
Re: Re: Re: Re: Create required fields from form
by sulfericacid (Deacon) on Jan 23, 2004 at 21:38 UTC
    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!
        1. Output the web page over again with an error message.
        2. Output the web page and use VALUE="foo" on your inputs so your users don't need to fill out the form again.
Re: Re: Re: Re: Create required fields from form
by hcb (Initiate) on Jan 24, 2004 at 20:53 UTC
    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.