Validate Email address





12
Date Submitted Tue. May. 9th, 2006 9:36 AM
Revision 1 of 1
Helper jpinkham
Tags Email | Java | Validate
Comments 1 comments
Another solution recommends using a text match pattern.
This works, but as long as you've got mail.jar in your classpath anyway, you might as well use this technique instead. (Plus, I think it works with older JDK 1.2+, whereas pattern.compile is 1.4+)

Note that it also accepts a comma-separated list of emails, just like a To: or CC: address line would.

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

public void onValidate(String value) throws InvalidEmail {
    try {
        InternetAddress.parse(value);
    } catch (AddressException e) {
        throw new InvalidEmail("Email: "+value+" is invalid.  Try something like myname@myhost.com");
    }
}
 

Jim Pinkham

Comments

Comments Internet Access?
Tue. May. 9th, 2006 12:38 PM    Coder mattrmiller

Voting