ASPN ActiveState Programmer Network  
ActiveState, a division of Sophos
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups
Submit Recipe
My Recipes

All Recipes
All Cookbooks


View by Category

Title: Checking valid e-mail address in the easy way
Submitter: Not specified Not specified (other recipes)
Last Updated: 2001/09/23
Version no: 1.0
Category:

 

Not Rated yet


Description:

This code Perl snipet is intended to check valid e-mail address, code is easy to understand because composed by step by step regex rather than full one line complicated regex, thus its makes each step may be easily modified as needed.

Contact me: tbyte@bolehmail.com

Usage: Text Source

sub ValidEmailAddr { #check if e-mail address format is valid

  my $mail = shift;                                                  #in form name@host

  return 0 if ( $mail !~ /^[0-9a-zA-Z\.\-\_]+\@[0-9a-zA-Z\.\-]+$/ ); #characters allowed on name: 0-9a-Z-._ on host: 0-9a-Z-. on between: @

  return 0 if ( $mail =~ /^[^0-9a-zA-Z]|[^0-9a-zA-Z]$/);             #must start or end with alpha or num

  return 0 if ( $mail !~ /([0-9a-zA-Z]{1})\@./ );                    #name must end with alpha or num

  return 0 if ( $mail !~ /.\@([0-9a-zA-Z]{1})/ );                    #host must start with alpha or num

  return 0 if ( $mail =~ /.\.\-.|.\-\..|.\.\..|.\-\-./g );           #pair .- or -. or -- or .. not allowed

  return 0 if ( $mail =~ /.\.\_.|.\-\_.|.\_\..|.\_\-.|.\_\_./g );    #pair ._ or -_ or _. or _- or __ not allowed

  return 0 if ( $mail !~ /\.([a-zA-Z]{2,3})$/ );                     #host must end with '.' plus 2 or 3 alpha for TopLevelDomain (MUST be modified in future!)

  return 1;
}

The license for this recipe is available here.

Discussion:

As ICANN already assigned Top Level Domain name more than 3 characters, user must be modified the last code fragment.



Add comment

Number of comments: 2

Please read RFC 822, Simon Flack, 2001/12/17
Please see the comments here: http://aspn.activestate.com/ASPN/Cookbook/Rx/Recipe/59886 and an alternative recipe here: http://aspn.activestate.com/ASPN/Cookbook/Rx/Recipe/65255

There are several perl modules on CPAN that do Email address checking: RFC::RFC822::Address, Email::Valid and Mail::CheckUser. Regexps such as the one you show here fail on some valid email addresses
Add comment

Why re-invent the wheel ?, ozzy osbourne, 2003/03/27
The best thing about PERL is the number of existing modules. -= ozzy =-
Add comment



Highest rated recipes:

1. Breaking down a URI into ...

2. Finding Palindromes

3. Extracting HTML URL Links

4. Removing dangerous ...

5. Matching Royal Mail ...

6. Finding URLs in text -- ...

7. Validating email ...

8. Validate Domain Names

9. Extract the Korean ...

10. Remove any HTML




Privacy Policy | Email Opt-out | Feedback | Syndication
© 2006 ActiveState Software Inc. All rights reserved.