I am looking for a universal email validator pattern that can be used in GWTJava. Especially I need to accept emails containing the following characters:
à, ç, é, è, ê, î, ï, ô, ù
I am looking for a universal email validator pattern that can be used in GWTJava. Especially I need to accept emails containing the following characters:
à, ç, é, è, ê, î, ï, ô, ù
Those characthers are not valid as per RFC822 or RFC2822 (which obsoletes the first one). They are though part of the draft RFC5335.
Regular expressions to validate emails are a bad idea, generally. They can go as complex as this one. So, usually I try to find a good compromise between result and code complexity. I like to do this:
try {
new InternetAddress(email, true);
} catch (AddressException e) {
return false;
}
InternetAddress
is a class contained in the JavaMail package and the true
params tells it to perform a strict validation. It works pretty well. Being it into the official JavaMail package, chances are that the implementation is kept up to date with new releases.
Those charachters are valid in the name of the sender, and this code handles it.
àndreas <andreas@gmail.com> // pass validation
àndreas@gmail.com // don't pass validation
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ValidatorUtil {
private static final String PATTERN_EMAIL = "^[_A-Za-z0-9-\\+ HERE PUT YOUR ADITIONAL CHARACTERS]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
/**
* Validate given email with regular expression.
*
* @param email
* email for validation
* @return true valid email, otherwise false
*/
public static void main(String args[])
{
String email = "aáaaa@gmail.com";
// Compiles the given regular expression into a pattern.
Pattern pattern = Pattern.compile(PATTERN_EMAIL);
// Match the given input against this pattern
Matcher matcher = pattern.matcher(email);
System.out.println(matcher.matches());
}
}