Special thanks to Bob Matsuoka for this one.
This is needed so often it should be available on every street corner.
[Thanks to Jason Bell, who noted that it should support mixed-case.]
“^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*$”
Here’s the Java code:
/**
* Uses a basic regular expression match to insure that
* the given email address "looks" like it should be valid.
* @param email
* @return true if valid, else false
*/
public static boolean isValidEmailAddress(String email)
{
String regex = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*$";
return email.matches(regex);
}
No related posts.
Cheers, needed this – nice one.
I can only second that… this is in my bookmarks now :)
isn’t it missing the dot in the second part?
it could look simpler like this:
“/^[\w]+(\.[\w-]+)*@[\w-]+\.(\.[\w-]+)*$/”
That last one doesn’t work for me. It fails to validate acceptable basic addresses like “test@test.com” using the JDK 1.4 Regex support.
The top regex does not take capital letters into account. Instead of a-z it should be A-z to capture all.
Cheers
Jase