Validate Email Using Perl
Hi,
Many forms these days which have email addresses will have a way to validate the email address syntax, and then, if it's wrong, send a message to the user asking them to put a correct email in. Also, the other fields of the form will still be filled in just as the user did, so he doesn't have to retype evrything. How is this done? I'm assuming it's easily done by perl. Is there some module or standard way of doing it, which can be added to any script which needs to validate an email address?
Thanks.
nike_guy_man posted this at 17:30 — 21st October 2001.
They have: 840 posts
Joined: Sep 2000
Hello
Here's how to send them back a page if its not correct syntax:
if ($FORM{'email'}) {
unless ($FORM{'email'} =~ /\w+@\w+.\w+/) {
print "Content-type: text/html\n\n";
print "<html><head><title>Bad E-mail</title></head>\n";
print "<body><h1>Bad E-mail</h1><br>The e-mail address that you've\n";
print "entered, $FORM{'email'}, is invalid. Please click back and\n";
print "try again.\n";
exit;
}
}
To keep them at the page, you could also do
if ($FORM{'email'}) {
unless ($FORM{'email'} =~ /\w+@\w+.\w+/) {
print "Content-type: text/html\n\n";
print "<html><head><title>Bad E-mail</title></head>\n";
print "<body><h1>Bad E-mail</h1><br>The e-mdail address that you've\n";
print "entered, $FORM{'email'}, is invalid. Please try again\n";
print "<form method='POST' action='form.cgi'> \n";
print "email: <input type=text name=email> \n";
print "<input type=text name=otherinfo value='$FORM{'otherinfo'}'> \n";
print "<input type=submit> \n";
exit;
}
}
Change $FORM{'otherinfo'} to whatever other info you want to pass along.
I tried this out... should work
rline posted this at 07:28 — 22nd October 2001.
They have: 40 posts
Joined: Oct 2001
Hey,
Thanks a lot Nike_guy_man!
nike_guy_man posted this at 19:50 — 22nd October 2001.
They have: 840 posts
Joined: Sep 2000
No problem
Did you test it a few times??
Let me know how it all goes and if all works fine with what you want.
Thanks
rline posted this at 20:29 — 22nd October 2001.
They have: 40 posts
Joined: Oct 2001
Yeah. I tested it before I wrote you back. I've learned from the past, when I would thank profusely the person who provided a solution, only to discover that it didn't work, or on reflection, I didn't have a clue what it meant!
So, yes, what you wrote works great. Perhaps there's only one more thing I could ask you: instead of taking them to a page which asks them to use the back button, how do I write code (perhaps using javascript) so that they get an alert box with the "bad email" message, and then when they click OK, they automatically get taken back to the form they just filled out?
nike_guy_man posted this at 22:37 — 22nd October 2001.
They have: 840 posts
Joined: Sep 2000
I've done the same, many a time... gotten help, think it'll work, thank and praise them, only to find it won't work and you have to go back
I'm not sure about writing it in JS, but I'll try with PERL.
I'll post what I can find later...
Did the second code I wrote for you contain the info from the past page?
If it does, you could simply have the script write back to itself and if it doesn't match, it will only show the error page and have the same info filled out, but if its correct, it'll send the user on.
For example, name the script formmail.cgi and have the form part of the script point to formmail.cgi also... it works with PHP i've done that before.
I'll mess around with this for a little while and see what I can come up with!
Good luck
rline posted this at 22:49 — 22nd October 2001.
They have: 40 posts
Joined: Oct 2001
Actually, since my last post, I figured out how to do it with JS, in case you're interested. I'd still prefer with perl, because not everyone will have JS turned on, but until you can come up with something, and in case you care(!), here it is:
if (email is a valid one) {
do the normal stuff...
}
else {
$error = "The email address you entered, $FORM{'email'}, is invalid. You will now be taken back to enter your email address. Thankyou.";
print "Content-type:text/html\n\n";
print "<script language=\"javascript\">alert(\"$error\")\;\n";
print "window.location=\"javascript:history.back()\"\;</script>";
}
...It throws up an alert box, and when they click OK, it goes back to the form. Pretty neat, but I still think perl will be better...I wait in anticiapation!
nike_guy_man posted this at 22:58 — 22nd October 2001.
They have: 840 posts
Joined: Sep 2000
I'm a bit confused...
Do you want the info to stay in the form or just go back?
sometimes going back loses some info.
With PERL you can save all the info.
You could also set up an alert on the form page that will not let them POST to the .cgi unless the form fields are valid.
I'm still looking for this script though.
rline posted this at 23:07 — 22nd October 2001.
They have: 40 posts
Joined: Oct 2001
I want the info to stay in the form AND go back. I tried it with the JS, and no info was lost.
This idea:
"You could also set up an alert on the form page that will not let them POST to the .cgi unless the form fields are valid."
sounds the best so far. That would make it even easier. But can that be done with perl?
nike_guy_man posted this at 23:09 — 22nd October 2001.
They have: 840 posts
Joined: Sep 2000
Not letting them submit is done with Javascript...
I've seen this used many times before and I've been looking for it for a little while now...
What type of file is the form-submitting page? Perl or HTML?
Can you give me a link, or PM or email it?
Thanks
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.