locked_user Thailand_for_YOU has asked for the wisdom of the Perl Monks concerning the following question:

I'm looking for assistance to incorporate the Email Validity Test in the existing script called "wel.cgi", I use in all forms on my website.
This Email Validity Test should check an email address just to see if it passes a simple syntax check. It should verify that Email address contains the @ sign and only letters numbers, dashes and periods after it. Also check to make sure the address ends in 2 or 3 letters after a period and allow for it to be enclosed in [] such as [209.249.147.129]. If the Email address was filled incorrectly, the script should produce the following notification: "The entered Email address is of an invalid format. Please correct it.", preferably using a javascript that will highlight the form's email input field.

I've found a script called email_check (enclosed below) that partly do the job.
Maybe someone can help me.

########################################### sub email_check { local($email) = $_[0]; # Check that the email address doesn't have 2 @ signs, a .., a @., a # .@ or begin or end with a . if ($email =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(\.$)/ || # Allow anything before the @, but only letters numbers, dashes # and periods after it. Also check to make sure the address ends # in 2 or 3 letters after a period and allow for it to be enclosed # in [] such as [164.104.50.1] ($email !~ /^.+\@localhost$/ && $email !~ /^.+\@\[?(\w|[-.])+\.[a-zA-Z]{2,3}|[0-9]{1,3}\]?$/)) { return(0); } # If it passed the above test, it is valid. else { return(1); } } 1; ###########################################
If someone needs the origial script (wel.cgi), I can send it by email or post here.

Replies are listed 'Best First'.
Re: Adding Email Validity Test to the existing script.
by Angel (Friar) on Jun 30, 2003 at 11:42 UTC

    Email::Valid

    It's what I use and it cataches most of the silly email errors such as "angel@aol" or "angel@hotmail" and it looks and passes the really complicated patterns that some places use for email addresses "sgt.angel@area55.nowhere.mil"

    As ripped out of my code

    sub update_email_address( $ ) { my $data; my $query; my $self = shift; if( !Email::Valid->address( $_[0] ) ) { $self->{'error_type'} = "email invalid"; $self->{'error_string'} = "E-mail address is invalid."; return( undef ); } #else if good so use it for something }
Re: Adding Email Validity Test to the existing script.
by BazB (Priest) on Jun 30, 2003 at 11:46 UTC

    The only way to really check if an email address is valid is to send something to it.

    Angel's suggestion (++) will allow you to check if the email address looks like a valid email address, but not if it actually is valid.

    Update: Abigail's RFC::RFC822::Address module seems to be an alternative email validation module.


    If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
    That way everyone learns.

Re: Adding Email Validity Test to the existing script.
by Abigail-II (Bishop) on Jun 30, 2003 at 11:57 UTC
    You make a big mistake if you think an email address only contains a @ sign, and only letters, numbers, dashes or periods. Any ASCII character can occur in an email address. And a surprising number of them don't need to be escaped or quoted.

    Abigail

      In fact, you can even have more than one @ sign, believe it or not. IUPUI is an Indiana/Purdue campus here in downtown Indianapolis. It is now so big that it has its own branch campus (everyone following this?), IUPUI at Columbus, which translates to "iupui@c.edu". An e-mail account there could be "smith@iupui@c.edu"

      (I should point out that this was true at one time, about ten years ago; it may well have changed in the interim.)

      --
      tbone1
      Ain't enough 'O's in 'stoopid' to describe that guy.
      - Dave "the King" Wilson

        This is called address routing. A valid syntax, but many MTA's no longer honour this - it's a bit too spammer friendly. But
        \@@abigail.nl

        is an email address with a valid syntax.

        Abigail

Re: Adding Email Validity Test to the existing script.
by adrianh (Chancellor) on Jul 01, 2003 at 21:21 UTC

    As well as Email::Valid take a look at Mail::CheckUser. In addition to checking the syntax of the address and checking for an MX record it does a bunch of network checks that can cull a lot of bogus addresses.