APPROACH-1
APPROACH-2
APPROACH-3
public void phoneNoValidator(FacesContext facesContext, UIComponent uIComponent, Object object) { String msg2 = ""; if (object != null) { String phnNo = object.toString(); int openB = 0; int closeB = 0; boolean closeFg = false; char[] xx = phnNo.toCharArray(); for (char c : xx) { if (c == '(') { openB = openB + 1; } else if (c == ')') { closeB = closeB + 1; } if (closeB > openB) { closeFg = true; } } if (openB != closeB || closeFg == true || (phnNo.lastIndexOf("(") > phnNo.lastIndexOf(")")) || (phnNo.indexOf(")") < phnNo.indexOf("("))) { msg2 = "Brackets not closed properly."; FacesMessage message2 = new FacesMessage(msg2); message2.setSeverity(FacesMessage.SEVERITY_ERROR); throw new ValidatorException(message2); } if (phnNo.contains("()")) { msg2 = "Empty Brackets are not allowed."; FacesMessage message2 = new FacesMessage(msg2); message2.setSeverity(FacesMessage.SEVERITY_ERROR); throw new ValidatorException(message2); } if (phnNo.contains("(.") || phnNo.contains("(-") || phnNo.contains("-)")) { msg2 = "Invalid Phone Number.Check content inside brackets."; FacesMessage message2 = new FacesMessage(msg2); message2.setSeverity(FacesMessage.SEVERITY_ERROR); throw new ValidatorException(message2); } openB = 0; closeB = 0; closeFg = false; String expression = "([0-9\\-\\+\\(\\)]+)"; CharSequence inputStr = phnNo; Pattern pattern = Pattern.compile(expression); Matcher matcher = pattern.matcher(inputStr); String error = "Invalid Phone Number"; System.out.println("Index of plus is--->" + phnNo.lastIndexOf("+")); System.out.println("Bracket index--->" + phnNo.charAt(0)); if (matcher.matches()) { if (phnNo.contains("++") || phnNo.contains("--")) { throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, error, "Can not contain two hyphen(--) or plus(++)")); } else if (phnNo.lastIndexOf("+") > 1) { throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, error, "Plus sign should be in proper place")); } else if (phnNo.lastIndexOf("+") == 1 && phnNo.charAt(0) != '(') { throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, error, "Plus sign should be in proper place")); } else if (phnNo.startsWith(" ") || phnNo.endsWith(" ")) { throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, error, "Space Not allowed at start and end")); } } else { throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, error, "Only numeric character,+,() and - allowed")); } } }
APPROACH-3
Use the below custom validator
public void emailValidator(FacesContext facesContext, UIComponent uIComponent, Object object) {
if(object!=null){
String name=object.toString();
String expression="^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
CharSequence inputStr=name;
Pattern pattern=Pattern.compile(expression);
Matcher matcher=pattern.matcher(inputStr);
String msg="Email is not in Proper Format";
if(matcher.matches()){
}
else{
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,msg,null));
}
}
}