Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Poor man's email address validation function

by Jorge_de_Burgos (Sexton)
on Jun 26, 2004 at 20:16 UTC ( #369867=snippet: print w/ replies, xml ) Need Help??

Description: I am pretty new to Perl. I already love it! I needed to validate what a human operator states it is his email address, and I needed to avoid module installation. I have been tuning this function for a while and it seems to be quite good both in picking out bad formed addresses and in giving very few false positives. I hope it can be useful to someone. I would thank those who can suggest improvements too.
sub es_email_mal_formado {
  my $x = $_[0];
  if ($x =~ m/[^\w\@\.\-]/)    {return "Incluye un carácter prohibido"
+};
  if ($x =~ m/\W\W/)           {return "Tiene dos no alfanuméricos seg
+uidos"};
  if ($x =~ m/\@.+\@/)         {return "Tiene más de una arroba"};
  if (! ($x =~ m/\w\@\w/))     {return "No tiene arroba precedida y se
+guida de alfanumérico"};
  if (! ($x =~ m/\w\.\w/))     {return "No tiene punto precedido y seg
+uido de alfanumérico"};
  if (! ($x =~ m/\@.+\./))     {return "No tiene punto en la parte que
+ sigue a la arroba"};
  if ($x =~ m/^\W/)            {return "Empieza en no alfanumérico"};
  if (! ($x =~ m/\.\w{2,4}$/)) {return "No tiene \"de 2 a 4 alfanuméri
+cos\" al final"};
  if ($x =~ m/(\@|\.).\.\w+$/) {return "Tiene una sola letra antes del
+ último punto"};
  return 0;
}
Comment on Poor man's email address validation function
Download Code
Replies are listed 'Best First'.
•Re: Poor man's email address validation function
by merlyn (Sage) on Jun 27, 2004 at 01:47 UTC
    That's utterly and completely wrong with respect to "forbidden characters". For example, it invalidates any email address that has a gatewayed "local part" that contains a foreign (non RFC822) address, or addresses that I use to keep mail from getting scraped. For example, my fund responder is no longer fund@stonehenge.com, but fund*@stonehenge.com, and you've just ruled that address out.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Thanks Randal for your comments. Can you show me an example of an email address that has a gatewayed local part that contains a foreign (non RFC822) address? I just cannot figure out what that looks like.
        fund*@stonehenge.com

        Or my e-mail address: gellyfish.com!jns@localhost

        ;-)

        /J\

Re: Poor man's email address validation function
by rob_au (Abbot) on Jun 26, 2004 at 22:56 UTC
    Some code based upon code from Email::Valid::Loose ...
    my $esc = '\\\\'; my $period = '\.'; my $space = '\040'; my $open_br = '\['; my $close_br = '\]'; my $nonASCII = '\x80-\xff'; my $ctrl = '\000-\037'; my $cr_list = '\n\015'; my $qtext = qq/[^$esc$nonASCII$cr_list\"]/; # " my $dtext = qq/[^$esc$nonASCII$cr_list$open_br$close_br]/; my $quoted_pair = qq<$esc>.qq<[^$nonASCII]>; my $atom_char = qq/[^($space)<>\@,;:\".$esc$open_br$close_br$ctrl$no +nASCII]/; my $atom = qq<$atom_char+(?!$atom_char)>; my $quoted_str = qq<\"$qtext*(?:$quoted_pair$qtext*)*\">; my $word = qq<(?:$atom|$quoted_str)>; my $domain_ref = $atom; my $domain_lit = qq<$open_br(?:$dtext|$quoted_pair)*$close_br>; my $sub_domain = qq<(?:$domain_ref|$domain_lit)>; my $domain = qq<$sub_domain(?:$period$sub_domain)*>; my $local_part = qq<$word(?:$word|$period)*>; my $Addr_spec_re = qr<$local_part\@$domain>; # Is an address valid? if ( $address =~ /^$Addr_spec_re$/o ) { # Yes } else { # No }

     

    perl -le "print unpack'N', pack'B32', '00000000000000000000001011100110'"

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://369867]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2015-08-01 02:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (285 votes), past polls