e-mail validation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Geoff Soper

    e-mail validation

    I've been looking for a simple way of checking that a string is an e-mail
    address. I don't need to check if the address exists, just if the format of
    the string matches. There seem to be lots of different methods, can someone
    suggest which is the best, with justification. I'm not looking for total
    accuracy, I'd much rather let a few false positives through than get any
    false negatives.

    Many thanks


  • Manuel Lemos

    #2
    Re: e-mail validation

    Hello,

    On 08/20/2003 07:14 PM, Geoff Soper wrote:[color=blue]
    > I've been looking for a simple way of checking that a string is an e-mail
    > address. I don't need to check if the address exists, just if the format of
    > the string matches. There seem to be lots of different methods, can someone
    > suggest which is the best, with justification. I'm not looking for total
    > accuracy, I'd much rather let a few false positives through than get any
    > false negatives.[/color]

    You may want to check these classes:



    It has basic regular expression based validation if you do not want to
    use the DNS or SMTP server based validation.


    Also regular expression based validation integrated with forms
    generation and validation on the client side using Javascript and on the
    server side using the class itself.


    --

    Regards,
    Manuel Lemos

    Free ready to use OOP components written in PHP


    Comment

    • Wolfgang 'Dreamguard' Nagele

      #3
      Re: e-mail validation

      > I've been looking for a simple way of checking that a string is an e-mail[color=blue]
      > address. I don't need to check if the address exists, just if the format[/color]
      of[color=blue]
      > the string matches. There seem to be lots of different methods, can[/color]
      someone[color=blue]
      > suggest which is the best, with justification. I'm not looking for total
      > accuracy, I'd much rather let a few false positives through than get any
      > false negatives.[/color]
      you could use the following regexp for a quick solution with preg_match().
      regexp:
      '/^([\._a-zA-Z0-9-]{1,}){1}@{1}([a-zA-Z0-9-]{1,}){1}\.{1}([a-zA-Z]{1,3}){1}$
      /'

      yours, dreamguard.


      Comment

      • Randell D.

        #4
        Re: e-mail validation


        "Geoff Soper" <news.20-08-03@alphaworks.c o.uk> wrote in message
        news:3f43f2ed$0 $955$cc9e4d1f@n ews.dial.pipex. com...[color=blue]
        > I've been looking for a simple way of checking that a string is an e-mail
        > address. I don't need to check if the address exists, just if the format[/color]
        of[color=blue]
        > the string matches. There seem to be lots of different methods, can[/color]
        someone[color=blue]
        > suggest which is the best, with justification. I'm not looking for total
        > accuracy, I'd much rather let a few false positives through than get any
        > false negatives.
        >
        > Many thanks
        >
        >[/color]

        This is a PHP version of a JavaScript I found that performs syntax checking
        on an email address... There is sufficient remarks in it to let you know the
        checks that it does - you just pass it an email address and it will return
        "TRUE" or "FALSE" pending if its valid or not. Note, I return my TRUE and
        FALSE as a string (ie, inside double quotes) and not as a CONSTANT or INT.

        Function (and an example of usage) is below:


        function verifyEmail($em ailAddress)
        {
        // Return "TRUE" if we believe $email is a valid email address,
        // else return "FALSE"

        // First - make sure it has an @ sign and ensure that each side
        // of the @ sign has enough characters to be a valid address
        $pos = strpos($emailAd dress, "@");
        if ($pos === false) { // note: three equal signs
        return("FALSE") ;
        }

        list($email, $domain)=explod e("@", $emailAddress);
        if( (strlen($email) ==0) || (strlen($domain )<2) )
        { return("FALSE") ; }

        // make sure the top level of the domain name is no less than
        // two characters, and no greater than four characters in order
        // to allow .uk, .com, .info, .net etc...
        $domains=explod e(".", $domain);

        // Make sure the right side of the @ sign (the domain) is made
        // up of at least two subdomains (ie @xyz.com and not just @com)
        if(count($domai ns)>1)
        { $tld=array_pop( $domains);
        // Make sure the top level domain is NOT less than 2 char in length
        // and not greater than 4 char in length
        if( (strlen($tld)<2 ) || (strlen($tld)>4 ) )
        { return("FALSE") ; }
        }
        return("TRUE");
        }

        $example=sco@la rge.com;
        if(verifyEmail( $example)=="FAL SE")
        {
        die("Email address $example is NOT valid.");
        }
        print("<hr>Emai l address $example is believed to be fine and dandy.<hr>");


        Comment

        • Nikolai Chuvakhin

          #5
          Re: e-mail validation

          "Geoff Soper" <news.20-08-03@alphaworks.c o.uk> wrote in message
          news:<3f43f2ed$ 0$955$cc9e4d1f@ news.dial.pipex .com>...[color=blue]
          >
          > I've been looking for a simple way of checking that a string is an e-mail
          > address. I don't need to check if the address exists, just if the format of
          > the string matches. There seem to be lots of different methods, can someone
          > suggest which is the best, with justification.[/color]

          It all depends... For example, do you want to support the "user at
          domain dot com" syntax? A slightly less wicked one: do you want to
          treat '"Geoff Soper" <news.20-08-03@alphaworks.c o.uk>' as a valid
          e-mail address or do you only want to deal with the 'news.20-08-
          03@alphaworks.c o.uk' part?

          Cheers,
          NC

          Comment

          Working...