Skip to content

Instantly share code, notes, and snippets.

@SakaDream
Created July 26, 2017 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SakaDream/9dc61acb0af6619c4aced355d698f112 to your computer and use it in GitHub Desktop.
Save SakaDream/9dc61acb0af6619c4aced355d698f112 to your computer and use it in GitHub Desktop.
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;
/**
* Created by Phan Ba Hai on 21/07/2017.
*/
@FacesValidator("com.sakadream.jsf.validator.PhoneValidator")
public class PhoneValidator implements Validator {
@Override
public void validate(FacesContext facesContext, UIComponent uiComponent, Object o) throws ValidatorException {
if(!o.toString().startsWith("0")) {
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Phone validation failed: ", "Invalid Phone Number!"));
} else if(o.toString().length() != 10 && o.toString().length() != 11) {
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "Phone validation failed: ", "Phone number must be 10 or 11 numbers!"));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment