David Graham <[EMAIL PROTECTED]> wrote:
> --- Jerry Jalenak <[EMAIL PROTECTED]> wrote:
> > Is there a published specification on e-mail addresses?  
> 
> I believe it's RFC 822.
> Unfortunately, I think that's a valid email address as defined in the RFC.

You're better off writing your own.  RFC 822 is very generous because it 
doesn't just deal with remote internet addresses, but email from all sorts 
of networks.

Here's an example of something that's a bit more restrictive.

This first runs it through javax.mail.internet.InternetAddress().  You'll 
need to have the javax.mail package installed to use it.

Then it performs additional restrictions (we want to disallow local 
addresses).


    private boolean isRemoteAddressSyntax(String emailAddress)
    {
        int firstAtPos = emailAddress.indexOf('@');
        int lastAtIndexOf = emailAddress.lastIndexOf('@');
        int lastDotIndexOf = emailAddress.lastIndexOf('.');

        // Must be one @
        if (-1 == firstAtPos)  return false;

        // Must be at least one character before @
        if (1 > firstAtPos)  return false;

        // Must be at least one '.'
        if (-1 == lastDotIndexOf)  return false;

        // Only one @ allowed.
        if (firstAtPos != lastAtIndexOf)  return false;

        // '.' must be at least one character after '@'
        if (lastDotIndexOf < (firstAtPos + 2))  return false;

        // Must be at least one character after '.'
        if (lastDotIndexOf == (emailAddress.length() - 1))  return false;

        return true;
    }

    public boolean isValidBasicEmailSyntax(String emailAddress)
    {
        try
        {
            javax.mail.internet.InternetAddress anAddress = new 
javax.mail.internet.InternetAddress(emailAddress);

            return isRemoteAddressSyntax(emailAddress);
        }
        catch(javax.mail.internet.AddressException anAddressException)
        {
            return false;
        }
    }


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to