JSF - AlphaNumericValidator

public class AlphaNumericValidator implements Validator
{
public void validate(FacesContext facesContext, UIComponent uiComponent,
        Object obj)
        throws ValidatorException {
        String errorMsg = "ALPHA_NUMERIC_EXCEPTION";
        final String strContent = (String) obj;
      
        Integer charCount = 0;
       
        if (strContent != null && strContent.length() != 0) {
             for (Integer i = 0; i < strContent.length(); i = i + 1) {
                 if(Character.toLowerCase(strContent.charAt(i)) >= 'a'
                         && Character.toLowerCase(strContent.charAt(i)) <='z'){
                     charCount = charCount + 1;
                 }
             }
             if (charCount == 0) {
                 FacesMessage message = new FacesMessage();
                 message.setDetail(errorMsg);
                 throw new ValidatorException(message);
             }
 }
}
 

0 comments: