Skip to content

Instantly share code, notes, and snippets.

@thaniaclair
Last active December 17, 2015 05:49
Show Gist options
  • Save thaniaclair/5561136 to your computer and use it in GitHub Desktop.
Save thaniaclair/5561136 to your computer and use it in GitHub Desktop.
Validador de email java.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
/**
* Validador de e-mail.
* @author thania
*/
public class EmailValidator {
private Pattern pattern = null;
private Matcher matcher = null;
public static final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
public EmailValidator() {
pattern = Pattern.compile(EMAIL_PATTERN);
}
/**
* Valida o e-mail.
* @param email texto com o email.
* @return <code>true</code> se válido, <code>false</code> caso contrário.
*/
public boolean isValid(final String email) {
if (StringUtils.isBlank(email)) return false;
matcher = pattern.matcher(email);
return matcher.matches();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment