Validate Email address
12
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.
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");
}
}
Comments
Voting
Votes Up
ASmith
bertheymans
dannyboy
i_kenneth
java_junkie
napyfab
Pio
RatNuShock
rugi
SCoon
sundaramkumar
wiz1705






=================
Matthew R. Miller
http://bluecreststudios.com
http://www.codeandcoffee.com