Java:Data Science Made Easy
上QQ阅读APP看书,第一时间看更新

Validating e-mail addresses

It is also common to need to validate e-mail addresses. While most e-mail addresses have the @ symbol and require at least one period after the symbol, there are many variations. Consider that each of the following examples can be valid e-mail addresses:

  • myemail@mail.com
  • MyEmail@some.mail.com
  • My.Email.123!@mail.net

One option is to use regular expressions to attempt to capture all allowable e-mail addresses. Notice that the regular expression used in the method that follows is very long and complex. This can make it easy to make mistakes, miss valid e-mail addresses, or accept invalid addresses as valid. But a carefully crafted regular expression can be a very powerful tool.

We use the Pattern and Matcher classes to compile and execute our regular expression. If the pattern of the e-mail we pass in matches the regular expression we defined, we will consider that text to be a valid e-mail address:

public static String validateEmail(String email) { 
String emailRegex = "^[a-zA-Z0-9.!$'*+/=?^_`{|}~-" +
"]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\." +
"[0-9]{1,3}\\])|(([a-zAZ\\-0-9]+\\.)+[a-zA-Z]{2,}))$";
Pattern.compile(emailRegex);
Matcher matcher = pattern.matcher(email);
if(matcher.matches()){
return email + " is a valid email address";
}else{
return email + " is not a valid email address";
}
}

We make the following method calls to test our data:

out.println(validateEmail("myemail@mail.com")); 
out.println(validateEmail("My.Email.123!@mail.net"));
out.println(validateEmail("myEmail"));

The output follows:

myemail@mail.com is a valid email address
My.Email.123!@mail.net is a valid email address
myEmail is not a valid email address

There is a standard Java library for validating e-mail addresses as well. In this example, we use the InternetAddress class to validate whether a given string is a valid e-mail address or not:

    public static String validateEmailStandard(String email){ 
try{
InternetAddress testEmail = new InternetAddress(email);
testEmail.validate();
return email + " is a valid email address";
}catch(AddressException e){
return email + " is not a valid email address";
}
}

When tested against the same data as in the previous example, our output is identical. However, consider the following method call:

    out.println(validateEmailStandard("myEmail@mail")); 

Despite not being in standard e-mail format, the output is as follows:

myEmail@mail is a valid email address

Additionally, the validate method by default accepts local e-mail addresses as valid. This is not always desirable, depending upon the purpose of the data.

One last option we will look at is the Apache Commons EmailValidator class. This class's isValid method examines an e-mail address and determines whether it is valid or not. Our validateEmail method shown previously is modified as follows to use EmailValidator:

public static String validateEmailApache(String email){ 
email = email.trim();
EmailValidator eValidator = EmailValidator.getInstance();
if(eValidator.isValid(email)){
return email + " is a valid email address.";
}else{
return email + " is not a valid email address.";
}
}