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 can enter invalid email addresses, and then wonder why they are not receiving email from you.
For example, people usually miss the .com
from their email address, as in bob@aol
or jane@juno
This Perl subroutine checks 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.
This subroutine does not check that the email address actually exists on the Internet, or that the domain name is valid, only that the format of the email address is correct. In our experience, this catches the majority of problems.
$a = "john\@example.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;
}