Lyris User's Guide
[previous] [next] [contents]
How do I validate Email Addresses in my Perl programs?
Table of Contents
· Introduction
· Email Commands
· Web Interface for Users
· Server Administrator
· Site Administrator
· List Administrator
· Other Topics
· Add-On Packages
· Installing and Upgrading
· Appendix
· Frequently Asked Questions
· · DocBots
· · Running Lyris List Manager
· · Email
· · Lyris List Manager Administration
· · Web Browsers
· · Usenet Newsgroups
· · International
· · Other FAQ issues
· · Mailing List Features
· · Perl/LCP Toolkit
· · · How do I validate Email Addresses in my Perl programs?
· · · Sample program - Import List of Email Addresses
· · · Sample Program: import mailing list archives
· · Unix Administration

How do I validate Email Addresses in my Perl programs?

If you are writing your own CGI form for subscribing people, we recommend that you validate the email addresses that are entered on the web form. Otherwise, people will often enter invalid email addresses, and then wonder why they are not receiving email from you. For example, a common mistake is for people to leave the ".com" off their address, as in "bob@aol" or "jane@juno".

This Perl subroutine will check to make sure the email address you give it looks valid, trim any starting or trailing white space and return the email address to you. If the email address was not valid, it will return a blank string.

Note: This subroutine does not check that the email address actually exists on the Internet, or that the domain name is valid, only that that the format of the email address is correct. In our experience, this catches the majority of problems.

$a = "john\@somewhere.com";
print &CheckEmailAddress($a);

sub CheckEmailAddress() {
# This regexp validates the format of an email address. It returns the cleaned
# version of the email address, or blank if the address was not valid.
#
# An email address must be of the form:
# 1) (trim any spaces or tabs or linefeeds or carriage returns)
# 2) (possibly one quotation mark)
# 3) (one or more characters for username, excepting a quotation mark)
# 4) (possibly one quotation mark)
# 5) @
# 6) (one or more characters for hostname(s), excepting [ <>\t])
# 7) .
# 8) (two or more characters for top-level-domain, excepting [ <>\t])
# 9) (trim any spaces or tabs or linefeeds or carriage returns)
#
# 1 2 3 4 56 7 8 9
# .............'''.......'''.'''''''''..''''''''''''''''''.............
$_[0] =~ /[ |\t|\r|\n]*\"?([^\"]+\"?@[^ <>\t]+\.[^ <>\t][^ <>\t]+)[ |\t|\r|\n]*/;
return $1;
}

Other pages which link to this page:
  • Perl/LCP Toolkit
  • Page 626 of 629