Created
July 26, 2017 09:48
-
-
Save SakaDream/7c08271dd88934926ce1942f8faa12e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sakadream.jsf.validator; | |
import javax.faces.application.FacesMessage; | |
import javax.faces.component.UIComponent; | |
import javax.faces.context.FacesContext; | |
import javax.faces.validator.FacesValidator; | |
import javax.faces.validator.Validator; | |
import javax.faces.validator.ValidatorException; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
/** | |
* Created by Phan Ba Hai on 21/07/2017. | |
*/ | |
@FacesValidator("com.sakadream.jsf.validator.EmailValidator") | |
public class EmailValidator implements Validator { | |
private 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,})$"; | |
private Pattern pattern; | |
private Matcher matcher; | |
public EmailValidator(){ | |
pattern = Pattern.compile(EMAIL_PATTERN); | |
} | |
@Override | |
public void validate(FacesContext facesContext, UIComponent uiComponent, Object o) throws ValidatorException { | |
matcher = pattern.matcher(o.toString()); | |
if(!matcher.matches()) { | |
FacesMessage msg = | |
new FacesMessage("E-mail validation failed:", | |
"Invalid E-mail format."); | |
msg.setSeverity(FacesMessage.SEVERITY_ERROR); | |
throw new ValidatorException(msg); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment