Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Email (https://www.askmehelpdesk.com/forumdisplay.php?f=107)
-   -   Properly formatted emails (https://www.askmehelpdesk.com/showthread.php?t=1850)

  • Apr 24, 2003, 06:25 PM
    Animal
    Properly formatted emails
    Hello,

    What determines a valid (or invalid) email address? I'm writing a script which can determine whether the formatting is correct or not. For example, you need an "@" sign, a ".", etc. You can't have spaces or backslashes in an email, etc.

    Does someone have a complete list of rules?

    Thanks,
    Steve
  • Apr 24, 2003, 06:43 PM
    cyrus
    Properly formatted emails
    Most java scripts that are written to check for a valid e-mail only check for the "@" and that it contains a "." that's it ;)
  • May 13, 2003, 05:47 PM
    zylstra
    Properly formatted emails
    From the CGI/Perl CookBook:

    Code:

    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
            # three 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);
        }
    }

    Here's some more Perl code:

    Code:

    $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;
    }

    Or, if you're using Perl you can use the module Mail::RFC822::Address.

    Or, if you like reading specs, it's supposed to be in RFC822, http://www.faqs.org/rfcs/rfc822.html, but I couldn't find it.

    -Z

  • All times are GMT -7. The time now is 04:58 PM.