
Jasmine
Administrator
/ Moderator
Jan 15, 2000, 12:11 PM
Post #6 of 18
(2604 views)
|
|
Re: Being selective with email addresses.
[In reply to]
|
Can't Post
|
|
While it is quite difficult (impossible) to use only one single check to ensure an email is valid, here's regex that checks for the most common email addresses. <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> if ($email =~ /^[\s]*[\w-.]+\@[\w-]+([\.]{1}[\w-]+)+[\s]*$/) { # it's a good email } else { # it's a "bad" email } </pre><HR></BLOCKQUOTE> The above will correctly match you@yourdomain.com as well as you.you@yourdomain.co.uk, the most common email forms. It will not pass addresses with spaces in it or other uncommon email address formats. As for checking to see if it's a hotmail account, you can use: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> $domain = (split(/@/,lc($email)))[1]; </pre><HR></BLOCKQUOTE> $domain now holds the domain name part of the email address (everything after the @). The lc($email) makes the email address lower case. Now, all you have to do is match $domain to whatever domain you want to disallow. Example: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> $forbidden = "hotmail.com"; if ($forbidden eq $domain){ # it's a forbidden email domain } else { # it's an allowed email domain } </pre><HR></BLOCKQUOTE> Now the above works just fine for a single forbidden domain. If you have several domains that you want to disallow, you may wish to place them all in a text file, depending on how many you have. Let's assume that you want to disallow all of the free email addresses you can find (good luck in finding them all! -- check the bottom of this reply for a good start). You don't want to edit your program each time you find a new one, so a data file would be ideal in this situation. As always, the best way to develop a program or a subroutine is to plan what you want it to do. Planning helps make your program more "logical" and efficient. So, a visitor submits an email address. First, you want to make sure that it's a "valid" email address. If it is, then you want to check to see if it's a forbidden email address. Consider the following: <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> #!/usr/bin/perl $email_db = "/path/to/freeemail.txt"; # server path to forbidden email database my $visitoremail = $FORM{'email'}; # form input from your visitor my $passedcheck = check_email($visitoremail); # The above line calls the check_email subroutine, and # passes the email address to be checked. # The subroutine will return a 0 for good emails, and 1 # for bad email addresses. my $passfail = $passedcheck ? "Failed Email Check" : "Passed Email Check"; # This line takes the results from $passedcheck, and assigns # $passfail with an English pass/fail message (instead of 0 # for pass and 1 for fail) print "$passfail\n\n"; # Simply prints if the address passed or failed. Add more code here. exit; ############################ # SUB - CHECK EMAIL # # Usage - check_email($emailtobechecked); # # Description: This subroutine takes the # email address passed to it, and checks # for validity of common email formats. # # If the address doesn't pass the common # email format, it returns a 1 to the # line calling the subroutine. # # If the address passes the common email # format, it grabs the domain name from # the email address, and invokes the # freemailcheck subroutine to prevent # defined forbidden email addresses from # passing. sub check_email { my $email = shift; #get the email address that was passed to the subroutine my $error = 0; # innocent until proved guilty if ($email =~ /^[\s]*[\w-.]+\@[\w-]+([\.]{1}[\w-]+)+[\s]*$/) { $domain = (split(/@/,$email))[1]; $error = freemailcheck($domain); } else { $error = 1; } return $error; } ############################ # SUB - FREE EMAIL CHECK # # Usage - freemailcheck($domaintobechecked); # # Description: This subroutine takes the # domain name address passed to it, and # checks the domain name against a database # of free email address. The path to this # database needs be defined in a variable # named $email_db. # # If the domain is listed in the database, # it returns a 1 to the line calling the # subroutine. # # If the domain is not listed in the # database, the initialized free_error = 0 # is returned to the calling subroutine. sub freemailcheck { my $domain = lc(shift); my $free_error = 0; #innocent until proven guilty open (DB,"<$email_db") or die "Couldn't open database $email_db - $!\n"; while (<DB> ) { chomp; if (lc($_) eq $domain){ $free_error = 1; last; } } close (DB); return $free_error; } </pre><HR></BLOCKQUOTE> If your goal is to prohibit all of the free email addresses you can find, you're welcome to grab our free email address file to get "started" -- there's already over 4,600 free email address domains in that database. If you choose to grab it, please be patient -- it's a 70k file. If you'd like any clarification on the code above, please feel free to post your question. This topic will be addressed in painstaking detail in the March issue of the Learning Center with line-by-line explanations of the code. Please let me know if this helped.
|