jsff code
<af:inputText styleClass="email" label=" " id="it1" rows="1" {pageFlowScope.TestBean.userEmail}"
validator="#{pageFlowScope.TestBean.customEmailValidator}"> </af:inputText>
Java Code
public void customEmailValidator(FacesContext facesContext,
UIComponent uIComponent, Object object) {
if (object != null) {
CharSequence inputStr = object.toString();
Pattern pattern = Pattern.compile(PortalConstants.EMAIL_PATTERN);
Matcher matcher = pattern.matcher(inputStr);
if (! matcher.matches()) {
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
PortalConstants.EMAIL_FORMAT_ERROR, null));
}
}
}
Constants Code
public class PortalConstants {
public static final String EMAIL_FORMAT_ERROR = "Please enter a valid email address";
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,})$";
}
<af:inputText styleClass="email" label=" " id="it1" rows="1" {pageFlowScope.TestBean.userEmail}"
validator="#{pageFlowScope.TestBean.customEmailValidator}"> </af:inputText>
Java Code
public void customEmailValidator(FacesContext facesContext,
UIComponent uIComponent, Object object) {
if (object != null) {
CharSequence inputStr = object.toString();
Pattern pattern = Pattern.compile(PortalConstants.EMAIL_PATTERN);
Matcher matcher = pattern.matcher(inputStr);
if (! matcher.matches()) {
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
PortalConstants.EMAIL_FORMAT_ERROR, null));
}
}
}
Constants Code
public class PortalConstants {
public static final String EMAIL_FORMAT_ERROR = "Please enter a valid email address";
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,})$";
}
No comments:
Post a Comment